Search Tutorials

Tuesday 23 April 2013

Java code to calculate factorial of a number using recursion

Java program to calculate 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:-

Java code to print factorial of a number using recursion

Enter a number : 5
Factorial = 120

Related Programs:-

Generate Fibonacci series using recursion

Find the HCF & LCM of two numbers using recursion

Find the sum of the following series using recursion : 12 + 22 + 32 +.......+ n2

Find the sum of Natural numbers up to a given number using Recursion

Calculate the Power of a number using recursion

No comments:

Post a Comment

Back to Top