> 其他 >
分别用三种循环语句(while语句、do-while语句、for语句)各编写一个程序实现求1 20的累乘,即:
1 × 2 × 3 × …… × 20
总结出三种循环语句哪种实现起来方便、灵活.
人气:157 ℃ 时间:2020-02-20 15:03:06
解答
//while写法
#include <stdio.h>
main()
{
    long total=1, i=1;
    while(i<=20)
    {
        total = total * i;
        i++;
    }
    printf("result=%d\n", total);
}
//do-while写法
#include <stdio.h>
main()
{
    long total=1, i=1;
    do
    {
        total = total * i;
        i++;
    } while(i<=20);
    printf("result=%d\n", total);
}
//for写法
#include <stdio.h>
main()
{
    long total=1, i;
    for(i=1; i<=20; i++)
        total = total * i;
    printf("result=%d\n", total);
}
推荐
猜你喜欢
© 2024 79432.Com All Rights Reserved.
电脑版|手机版