Answer to Question #320545 in C for Pratik

Question #320545

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.

1
Expert's answer
2022-03-30T02:50:56-0400
#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);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS