Write 4 versions (in C) of a program that computes the sum of the first n even (positive) integers and the sum of the first n odd (positive) integers. The value for n should be entered interactively. In the first version of your program, use the code
for (cnt = 0, i = 1, j = 2; cnt < n; ++cnt, i+= 2, j += 2) odd_sum += i, even_sum += j;
Note the prolific use of the comma operator. The code is not
very good, but it will give you confidence that the comma operator
works as advertised! In the second version, use one or more for
statements but no comma operators. In the third version,
use only while statements. In the fourth version, do not
use any loops ! In other words, find a closed form for
the sums.
Back to main page