import java.util.Scanner;
public class factorial {
public static void main(String [] args){
Scanner in = new Scanner(System.in);
int x = 0, y;
System.out.println("enter factorial number");
if(in.hasNextInt()){
x = in.nextInt();// check if the scanner's next token is an int
}//end if
y = factorial(x);
System.out.println("factorials is "+y);
}//end main
private static int factorial(int n) {
// TODO Auto-generated method stub
if(n==0||n==1){
return 1;
}
else
return n*factorial(n-1);//recursive call
}//end factorial
}//end class
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
227 次 |
| 最近记录: |