While summing up a bunch of numbers without knowing the how many numbers in advance, then we will design a special number, usually \(-1\), to denote the end of the numbers.
/* Description: This program is to sum up a bunch of numbers. Author: Liutong Xu Date: 2016/10/24 */ #include <stdio.h> int main() { int a; int sum; sum = 0; scanf("%d",&a); while (a != -1) { sum = sum + a; scanf("%d",&a); } printf("The sum is %d.\n",sum); return 0; }
stdin
1 2 3 4 5 -1
stdout
The sum is 15.