Wednesday, 11 January 2017

Recursion Program to find Factorial of a number

import java.util.Scanner;
public class Factorial {
public static void main(String[] args)
{
System.out.println(“Enter the number”);
Scanner sc = new Scanner(System.in);
int num=sc.nextInt();
System.out.println(fact(num));
}
public static int fact(int x)
{
if(x==1)
{
return 1;
}
else
{
return x*fact(x-1);
}
}
}
Output :-
Enter the number :
5
120

No comments:

Post a Comment