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.

[sourcecode 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 *
*******************************************************/

#include
#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 is %d\n”, sum);
}[/sourcecode]

0 Shares
Share
+1
Tweet
Share
Stumble