Answer to Question #320543 in C for Pratik

Question #320543

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:51:07-0400
#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);
}

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