C語言程式設計-現代方法 第二版 第7.1.5小節程式碼 數列求和改進版
阿新 • • 發佈:2020-12-13
第7.1.5小節程式碼 數列求和改進版
//This is a comment
//Author:King
//Time:2020/12/5
//Reference:C Programming:A Modern Approach,Second Edition
/***************************************************************
7.1.5小節程式碼 數列求和改進版,變數改為long型支援更大的整數求和避免溢位
****************************************************************/
#include <stdio.h>
int main(void)
{
long n,sum = 0;
printf("This program sums a series of integers.\n");
printf("Enter integers (0 to terminate): ");
scanf("%ld",&n);
while(n != 0) //輸入0自動終止
{
sum += n;
scanf("%ld",&n);
}
printf ("The sum is : %ld\n",sum);
system("pause"); //加入該函式後可以使得產生的exe單獨執行,不會發生閃退。也可以加入其它函式使得main函式無法返回即可。如while(1)、getchar() 等
return 0;
}