分别用三种循环语句(while语句、do-while语句、for语句)各编写一个程序实现求1 20的累乘,即:
题目
分别用三种循环语句(while语句、do-while语句、for语句)各编写一个程序实现求1 20的累乘,即:
1 × 2 × 3 × …… × 20
总结出三种循环语句哪种实现起来方便、灵活.
答案
//while写法
#include <stdio.h>
main()
{
long total=1, i=1;
while(i<=20)
{
total = total * i;
i++;
}
printf("result=%d
", total);
}
//do-while写法
#include <stdio.h>
main()
{
long total=1, i=1;
do
{
total = total * i;
i++;
} while(i<=20);
printf("result=%d
", total);
}
//for写法
#include <stdio.h>
main()
{
long total=1, i;
for(i=1; i<=20; i++)
total = total * i;
printf("result=%d
", total);
}
举一反三
已知函数f(x)=x,g(x)=alnx,a∈R.若曲线y=f(x)与曲线y=g(x)相交,且在交点处有相同的切线,求a的值和该切线方程.
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
最新试题
热门考点
- 如图,在△ABC中,AD是∠BAC平分线,AD的垂直平分线分别交AB、BC延长线于F、E.求证: (1)∠EAD=∠EDA; (2)DF∥AC; (3)∠EAC=∠B.
- 有关函数的
- 有没有描写山顶的句子
- 我的爸爸和我的妈妈一样爱我.用英语怎么说,
- 连词成句,句型转换
- 根据28*65=1890,在下面的()里填上合适的数,你能想出几种填法?
- 甲、乙、丙三个数的平均数是70,甲数与乙数的比是5:2,乙数与丙数的比是4:7,求甲、乙、丙三个数各是多
- 英语翻译
- maybe someday you will fall in love with me and magybe one day you leave would
- 如图,P是正三角形ABC内一点,且PA=8,PB=6,PC=10,求三角形ABC面积?