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;
printf("\nUsing a do---- while statement:\n");
sum = 0;
i = 2;
do{
sum += i;
i = i + 3;
} while (i<100);
printf("Sum = %d\n", sum);
printf("\nUsing a for statement:\n");
sum = 0;
for (i=2; i<100; i=i+3)
{
sum += i;
}
printf("Sum = %d", sum);
}
Comments
Leave a comment