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

Greetings, everyone i have trouble with this exercise in programming C. Write a

ID: 3838604 • Letter: G

Question

Greetings, everyone i have trouble with this exercise in programming C. Write a do/while loop that computes the sum of all numbers between 23 and 509 (inclusive): 23 + 24 + 25 + … +509. The sum should be placed/stored in a variable that you will need to declare. You may only use two variables to complete this question. Be sure to declare and initialize ALL variables.

my ans was (it was wrong):

int main()
{
int n, sum=0;
scanf("%d",&n);
do /* while loop terminates if count>n */
{
sum+=n; /* sum=sum+count */
++n;
}(n >22 && n == 509);
  
printf("Sum = %d",sum);
return 0;
}

Explanation / Answer

Hi,

I have fixed the issue and highlighted the code changes below

#include <stdio.h>
#include <math.h>
int main()
{
int n=23, sum=0;
do /* while loop terminates if count>n */
{
sum+=n; /* sum=sum+count */
n++;
}while(n<= 509);
  
printf("Sum = %d ",sum);
return 0;
}

Output

gcc -o main *.c                                                                                                                                                                                                                                    

sh-4.2$ main                                                                                                                                                                                                                                                           

Sum = 129542