例5.1 求1+2+3+4+…+100.
# include <stdio.h>
int main ()
{int i=1,sum=0;
while(i<=100)
{sum=sum+i;
i++;
}
printf("sum=%d\n",sum);
return 0;
}
运行结果如下:
例5.2 用do…while语句求1+2+3+4+…+100。
# include <stdio.h>
int main ()
{int i=1;sum=0;
do
{sum=sum+i;
i++;
}while(i<=100)
printf("sum=%d\n",sum);
return 0;
}
运行结果如下:
例5.3 while和do…while循环的比较。
(1)用while循环:
# include <stdio.h>
int main()
{int i,sum=0;
printf("please enter i,i=?");
scanf("%d",&i);
while(i<=10)
{sum=sum+i;
i++;
}
printf("sum=%d\n",sum);
return 0;
}
运行结果如下(两次):
(2)用do…while循环:
# include <stdio.h>
int main()
{int i,sum=0;
printf("please enter i,i=?");
scanf("%d",&i);
do
{sum=sum+i;
i++;
}while(i<=10);
printf("sum=%d\n",sum);
return 0;
}
运行结果如下(两次):
例5.4 在全系1000名学生中举行慈善捐募,当总数达到10万元时就结束,统计此时捐募人数以及平均每人捐赠的数目。
# include <stdio.h>
# define SUM 100000
int main()
{
float amount,aver,total;
int i;
for(i=1,total=0;i<=1000;i++)
{
printf("please enter amount:");
scanf("%f",&amount);
total=total+amount;
if(total>=SUM) break;
}
aver=total/i;
printf("num=%d\naver=%10.2f\n",i,aver);
return 0;
}
运行结果如下:
例5.5 要求输出100~200的不能被3整除的数。
# include <stdio.h>
int main()
{
int n;
for(n=100;n<=200;n++)
{
if(n%3==0)
continue;
printf("%d",n);
}
printf("\n");
return 0;
}
运行结果如下:
例5.6 输出以下4*5的矩阵。
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
# include <stdio.h>
int main()
{
int i,j,n=0;
for(i=1;i<=4;i++)
for(j=1;j<=5;j++,n++)
{if(n%5==0) printf("\n");
printf("%d\t",i*j);
}
printf("\n");
return 0;
}
运行结果如下:
例5.7 用公式求pai的近似值,知道发现某一项的绝对值小于1/1000000为止。
# include <stdio.h>
# include <math.h>
int main ()
{int sign=0;
double pi=0.0,n=1.0,term=1.0;
while(fabs(term)>=1e-6)
{ pi=pi+term;
n=n+2;
sign=-sign;
term=term/n;
}
pi=pi*4;
printf("pi=%10.8f\n",pi);
return 0;
}
运行结果如下:
例5.8 求Fibonacci(斐波那契)数列的前40个数。这个数列有如下特点:第1,2两个数为1,1.从第三个数开始,该数是其前两个数之和。即该数列为1,1,2,3,5,8,13,,,用数学方式表示为
# include <stdio.h>
int main()
{int f1=1,f2=1,f3;
int i;
printf("%12d\n%12d\n",f1,f2);
for(i=1;i<=38;i++)
{f3=f1+f2;
printf("%12d\n",f3);
f1=f2;
f2=f3;
}
return 0;
}
运行结果如下:
# include <stdio.h>
int main()
{int f1=1,f2=1;
int i;
for(i=1;i<=20;i++)
{printf("%12d%12d",f1,f2);
if(i%2==0) printf("\n");
f1=f1+f1;
f2=f2+f1;
}
return 0;
}
运行结果如下:
例5.9 输入一个大于3的整数n,判断它是否为素数(prime,又称质数)
# include <stdio.h>
int main()
{int n,i;
printf("please enter a integer number,n=?");
scanf("%d",&n);
for(i=2;i<n;i++)
if(n%2==0) break;
if(i<n) printf("%d is not a prime number.\n",n);
else printf("%d is a prime number.\n",n);
return 0;
}
运行结果如下:
# include <stdio.h>
# include <math.h>
int main()
{int n,i,k;
printf("please enter a integer number,n=?");
scanf("%d",&n);
k=sqrt(n);
for(i=2;i<=k;i++)
if(n%i==0) break;
if(i<k) printf("%d is not a prime number.\n",n);
else printf("%d is a prime number.\n",n);
return 0;
}
运行结果如下:
例5.10 求100~200的全部素数。
# include <stdio.h>
# include <math.h>
int main()
{int n,k,i,m=0;
for(n=101;n<=200;n=n+2);
{k=sqrt(n);
for(i=2;i<=k;i++)
if(n%i==0) break;
if(i>=k+1)
{printf("%d",n);
m=m+1;
printf("\t");
}
if(m%10==0) printf("\n");
}
printf("\n");
return 0;
}
运行结果如下:
例5.11 译密码。为使电文保密往往按一定规律将其转换成密码,收报人在按约定的规律将其译回原文。例如,可以按照以下规律将电文变成密码:将密码A变成字母E,a变成e,即变成其后的4个字母,W变成A,X变成B,Y变成C,Z变成D。字母按上述规律转换,非字母字符保持原状不变,如“China!”转换为“Glmre!”。
# include <stdio.h>
int main()
{char c;
c=getchar();
while(c!='\n')
{if((c>='a' && c<='z') || (c>='A' && c<='Z'))
{if(c>='w' && c<='z' || c>='W' && c<='Z') c=c-22;
else c=c+4;
}
printf("%c",c);
c=getchar();
}
printf("\n");
return 0;
}
运行结果如下:
# include <stdio.h>
int main()
{char c;
whlie((c=getchar())!='\n')
{if((c>='A' && c<='Z') || (c>='a' && c<='z'))
{c=c+4;
if(c>='Z' && c<='Z'+4 ||c>'z')
c=c-26;
}
printf("%c",c);
}
printf("\n");
return 0;
}
运行结果如下: