Write a loop that will calculate the sum of every third integer beginning with i = 2 (i-e. calculate the sum of 2+5+8+11+) for all values of i that are less than 100, Write the loop in
two ways:
(1) Using a do---- while statement.
() Using a for statement.
#include <stdio.h>
int main()
{
int i, sum;
// Using a do---- while statement
sum = 0;
i = 0;
do{
if (i % 3 == 0)
sum += (i+2);
i++;
} while (i<98);
printf("Sum = %d\n", sum);
// Using a for statement
sum = 0;
for (i=0; i<98; i++)
if (i % 3 == 0)
sum += (i+2);
printf("Sum = %d", sum);
}
Comments
Leave a comment