语句、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
发布评论