Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Solve the following problem three different ways, by first applying a while loop

ID: 3559436 • Letter: S

Question

Solve the following problem three different ways, by first applying a while loop, then a do/while loop, and finally a for loop. The problem is to find the sum of all integers from m through n. For example, if m is 4 and n is 7, this sum is 4+5+6+7 = 22. You may assume that m will always be less than or equal to n. There is no need to print anything. Just find the sum. Imagine the following statements have already been executed before your solutions start: Write your while loop solution here. Write your do/while loop solution here. Write your for loop solution here.

Explanation / Answer

#include <stdio.h>
int main()
{
   int m,n;
   int i;
   int sum = 0;
   printf("enter m,n");
   scanf("%i %i",&m,&n);
  
   //a) using while loop
   while(m <= n)
   {
       sum = sum + m;
       m++;
   }

  
   // b) using do/while loop
   do
   {
       sum = sum + m;
       m++;
   }
   while(m <= n);

  
   //c) using for loop
for(i = m; i <= n; i++)
   {
       sum = sum + i;
   }

  
   return 0;
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote