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. Given th

ID: 3838626 • Letter: G

Question

Greetings, everyone i have trouble with this exercise in programming C. Given that two integer variables, total and value, have been declared, write while a loop that reads values from the keyboard into value and adds them to total. The loop terminates when -1 is read into value. Don't forget to make sure that -1 is not added to total.

my ans was (it was wrong):

#include <stdio.h>

int main()
{
int n, sum=0;
printf("Enter an integer: ");
scanf("%d",&n);
while(n!=-1)   
{
sum+=n; /* sum=sum+count */
++n;
}
printf("Sum = %d",sum);
return 0;
}

Explanation / Answer

You missed to scan the values continuosly in the loop. You have to scan in the while loop

#include <stdio.h>

int main()
{
int n, sum=0;
printf("Enter an integer: ");
scanf("%d",&n);
while(n!=-1)   
{
sum+=n; /* sum=sum+count */
++n;

       scanf("%d",&n); //We have to scan continuosly in the loop.
}
printf("Sum = %d",sum);
return 0;
}