Java code to print the factorial of a number using recursion

/*
Program to print the factorial of a number using recursion.
*/
import java.io.*;
class factorial
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
factorial call = new factorial();
System.out.print("Enter a number : ");
int n = Integer.parseInt(br.readLine());
System.out.println("Factorial = " +call.fact(n));
}
int fact(int n)
{
if(n<2)
return 1;
else
return n*fact(n-1);
}
}

/**
* ALGORITHM :
* ---------
* 1. Start
* 2. Accept a number n from the user.
* 3. Using method of recursion, multiply all the numbers upto n.
* 4. Print the Factorial.
* 5. End
*/

/*
OUTPUT :
------
Enter a number : 5
Factorial = 120
*/


Related Posts by Categories

0 komentar:

Posting Komentar