脚本宝典收集整理的这篇文章主要介绍了第六次作业,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
1编写程序,使用while语句求和sum=1+3+5+…+21
#include<stdio.h>
main()
{
int i=1,sum=0;
while(i<=21)
{
sum+=i;
i+=2;
}
printf("%dn",sum);
}
2.编写程序,使用while语句求和sum=1+1/3+1/5+..+1/n
#include<stdio.h>
main()
{
int i=1,n;
double sum=0;
scanf("%d",&n);
while(i<=n)
{
sum+=1.0/i;
i+=2;
}
printf("sum=%fn",sum);
}
3编写程序,使用do-while语句求阶乘f=1*2*3*..*n
#include<stdio.h>
main()
{
int i=1,n,sum=1;
scanf("%d",&n);
while(i<=n)
{
sum*=i;
i++;
}
printf("%dn",sum);
}
4.打印出所有的水仙花数
#include<stdio.h>
main()
{
int i=100,ge,shi,bai;
while(i<=999){
ge=i%10;
shi=i/10%10;
bai=i/100;
if(i==ge*ge*ge+shi*shi*shi+bai*bai*bai)
printf("%4d",i);
i++;
}
}
5.输入两个数,输出他们中间的数
#include<stdio.h>
main()
{
int a,b,i;
scanf("%d%d",&a,&b);
i=a+1;
while(i<=b-1){
printf("%d",i);
i++;
}
}
6.输出1-100既能被3又能被5整除的数的和
#include<stdio.h>
main()
{
int n,i=0,sum=0;
scanf("i%3==o&&i%5==o",i);
while(i<=100){
sum+=i;
printf("%d",i);
i++;
}
}
以上是脚本宝典为你收集整理的第六次作业全部内容,希望文章能够帮你解决第六次作业所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。