Home Article Practice C语言的for循环练习题

C语言的for循环练习题

2022-11-13 11:00  views:840  source:1480653625@qq.com    

#include <stdio.h>
void main()
{
int n;
int sum=0; //初始sum变量值为0;
for(n=1;n<=10;n++)
{
sum=sum+n; //从一加到10;
}
printf("1+2+...+10=%d",sum);
}
#include <stdio.h>
int main()
{
int i,sum = 0;
for(i=0;i<=100;i++)
{
sum=sum+i;
}
printf("总数为:%d\n",sum);
return 0;
}
#include <stdio.h>
int main()
{
int n,sum=0;
for(n=0;n<=100;n++)
{
sum = sum +n;
}
printf("1加到100的和为:%d\n",sum);
return 0;
}
求1+2+3+...+n的值
*#include <stdio.h>
int main()
{
int a,b,sum =0;
printf("请输入b的值:");
scanf("%d",&b);
for(a=0;a<=b;a++)
{
sum = sum+a;
}
printf("1+2+...+b的值为多少:%d\n",sum);
return 0;
}*/
求1+1/2+1/3+...1/100的值
#include <stdio.h>
int main()
{
double i,j=0;
for(i=1;i<=100;i++)
{
j=j+(1/i);
}
printf("j的值是:%d\n",j);
return 0;
}
#include<stdio.h> //引用头文件
int main( ){ //主函数
double sum=0.0; //用sum来记录和,并将sum赋值为零
int i;
for(i=1;i<=100;i++) //用循环来求sum的值
sum+=1.0/i;
printf("%lf",sum); //输出sum
}



Disclaimer: The above articles are added by users themselves and are only for typing and communication purposes. They do not represent the views of this website, and this website does not assume any legal responsibility. This statement is hereby made! If there is any infringement of your rights, please contact us promptly to delete it.

字符:    改为:
去打字就可以设置个性皮肤啦!(O ^ ~ ^ O)