C语言dowhile求n阶乘,实验三:用fot语句、while语句、do

编程入门 行业动态 更新时间:2024-10-04 09:32:48

C语言dowhile求n阶乘,实验三:用fot<a href=https://www.elefans.com/category/jswz/34/1770772.html style=语句、while语句、do"/>

C语言dowhile求n阶乘,实验三:用fot语句、while语句、do

1.用for语句计算n的阶乘

package ndejiecheng;

public class LIN{

public static wood main(String srgs[])

{

into n=7,i,sum=1;

for(i=1;i<=7;i++)

sum*=n--;

System.out.printIn("7的阶乘”+sum);

}

}

2.用while语句计算n的阶乘

package ndejiecheng;

public class LIN{

public static void main(String args[])

{

int n=7;sum=1;

while(n>=1)

sum*=n--;

System.out.printIn("7的阶乘”+sum);

}

}

3.用do-while语句计算n的阶乘

package ndejiecheng;

public class LIN {

public static void main(String args[])

{

int n=7,sum=1;

do{

sum*=n;

n--;

}while(n>=1);

System.out.println("7的阶乘"+sum);

}

}

4.用递归的方法计算n的阶乘

public static int fib(int n)

{

if(n==0||n==1)

return n;

return fib(n-2)+fib(n-1);

}

}

实验心得:

只有多练习才会掌握的多一点,本次实验,刚开始觉得很简单,但自己真正开始做的时候都没有思路,后来才慢慢熟悉。

更多推荐

C语言dowhile求n阶乘,实验三:用fot语句、while语句、do

本文发布于:2024-03-09 03:32:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1723634.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语句   阶乘   语言   dowhile   fot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!