#include
int main()
{
int i=1,sum=0;
while(i<=100)
{
sum=sum+i;
i++;
}
printf("sum=%d\n",sum);
return 0;
}
运行结果如下:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190202200254850.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NTkyOTk2,size_16,color_FFFFFF,t_70)
## 例5.2用do...while语句运行1+2+3+...+100
```c
#include
int main()
{
int i=1,sum=0;
do
{
sum=sum+i;
i++;
}
while(i<=100);
printf("sum=%d\n",sum);
return 0;
}
#include
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;
}
#include
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;
}
#include
#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;
}
#include
int main()
{
int n;
for(n=100;n<=200;n++)
{
if(n%3==0)
continue;
printf("%d ",n);
}
printf("\n");
return 0;
}
#include
int main()
{
int i,j,n;
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;
}
#include
#include
int main()
{
int sign=1;
double pi=0.0,n=1.0,term=1.0;
while(fabs(term)>=1e-6)
{
pi=pi+term;
n=n+2;
sign=-sign;
term=sign/n;
}
pi=4*pi;
printf("pi=%10.8f\n",pi);
return 0;
}
F1=1 (n=1)
F2=1 (n=2)
Fn=Fn-1+Fn-2 (n>=3)
这是一个有趣的数学古典问题:有一对兔子,从出生后3个月起每个月都生一对小兔子,小兔子长到第三个月后每个月又生一对小兔子,假设所有兔子都不死问每个月的兔子总数为多少?
#include
int main()
{
int f1=1,f2=1,f3,i;
printf("%12d%12d",f1,f2);
for(i=1;i<=38;i++)
{
f3=f1+f2;
printf("%12d\n",f3);
f1=f2;
f2=f3;
}
printf("\n");
return 0;
}
#include
int main()
{
int n,i;
printf("please enter a integer number,n=?",n);
scanf("%d",&n);
for(i=2;i<n;i++)
if(n%i==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
#include
int main()
{
int n,k,i,m=0;
for(n=101;n<=200;n++)
{
k=sqrt(n);
for(i=2;i<=k;i++)
if(n%i==0)break;
if(i>=k+1)
{
printf("%d ",n);
m=m+1;
}
if(m%10==0)printf("\n");
}
printf("\n");
return 0;
}
#include
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;
}