This C Language program reads a sequence of positive integers from the console, given by the user, and prints them out together with their sum. Use a Sentinel value (say 0) to determine when the sequence has terminated. So wehenever the user will enter 0 the program will stop taking input and calculate the sum and out put it on the screen.

[code language=cpp]/*******************************************************
* MYCPLUS Sample Code – http://www.mycplus.com *
* *
* This code is made available as a service to our *
* visitors and is provided strictly for the *
* purpose of illustration. *
* *
* Please direct all inquiries to saqib at mycplus.com *
*******************************************************/

//Read a sequence of positive integers and
//print them out together with their sum.
//Use a Sentinel value (say 0) to determine when the sequence has terminated.

#define SENTINEL 0
int main(void) {
int sum = 0; /* The sum of numbers already read */
int current; /* The number just read */
do {
printf(“\nEnter an integer > “);
scanf(“%d”, &t);
if (current > SENTINEL){
sum = sum + current;
}
} while (current > SENTINEL);
printf(“\nThe sum of all the numbers is = %d\n”, sum);
}[/code]

0 Shares
Share
+1
Tweet
Share
Stumble