计算sum=的值。
#includeint main() { int i,sum; i=1;sum=0; while(i<=100) { sum=sum+i; i+=1; } printf("sum=%dn",sum); return 0; }
运行结果:
输入一个正整数 n,计算n!
#includeint main() { int i;long n,fact; i=2;fact=1; printf("请输入n的值:"); scanf("%ld",&n); while(i<=n) { fact=fact*i; i=i+1; } printf("%ld!=%ldn",n,fact); return 0; }
运行结果:
由键盘输入一串字符,分别统计输入字符中数字字符,字母字符及其他字符的个数。
#includeint main() { int digit,letter,other; char ch; digit=letter=other=0; printf("请输入一串字符:"); while((ch=getchar())!='n') if((ch>='0')&&(ch<='9')) digit++; else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) letter++; else other++; printf("数字%d个,字母%d个,其他%d个n",digit,letter,other); return 0; }
运行结果:



