阶乘、斐波那契数列(java版)
1 public class Demo 2 { 3 public static void main(String[] arg){ 4 5 System.out.println("10的阶乘是:"+fact(10)); 6 7 System.out.println(""); 8 9 System.out.print("斐波那契数列:"); 10 11 int count=12; 12 for(int i=0;i<count;i++){ 13 if(i<count-1){ 14 System.out.print(fib(i)+","); 15 }else{ 16 System.out.print(fib(i)); 17 } 18 } 19 } 20 21 //阶乘 22 private static int fact(int num){ 23 if(num==0 || num==1){ 24 return 1; 25 } 26 return num*fact(num-1); 27 } 28 29 //斐波那契数列 30 private static int fib(int num){ 31 if(num<2)return 1; 32 return fib(num-1)+fib(num-2); 33 } 34 }
运行结果: