TestingDocs.com
Testing Questions
  • Home
  • Automation
    • Selenium
    • TestNG
  • Tutorials
    • MySQL Tutorials
    • TestLink
    • Maven
    • Git
  • Flowcharts
    • Flowgorithm
    • Raptor

Java Programs

Factorial Java Program using Recursion

Program Description

Write a java program to compute factorial of given number n using Recursion.

IPO Chart

Input

We will take input from the user for n

Process 

For n>= 0 , we will do a recursive call using the below formula

fact(n) = n* fact(n-1)

Output

factorial of n.

 

 

 

Java Program

package recursion;

import java.util.Scanner;

/**************************************************
 * FactorialRecursion.java
 * @program : Factorial Using Recursion
 * @web : www.TestingDocs.com
 * @author : 
 * @version : 
 **************************************************/
public class FactorialRecursion {
 //Main method
 public static void main(String[] args) {
 int n;
 Scanner input = new Scanner(System.in);
 System.out.println("Enter positive n:= ");
 n = input.nextInt();

 if(n >= 0)
 System.out.println("Factorial of " + n + " is = " + factRecurFun(n));
 else
 System.out.println("Enter valid input.");
 }

 /**************************************************
 * factRecurFun() is a recursive method to compute
 * factorial of the given number.
 **************************************************/
 public static int factRecurFun(int n) {
 // base case
 if(n < 1 ) 
 return 1;
 else
 //Recursive call
 return n*factRecurFun(n-1);
 }
}

 

Program Output

Enter positive n:=
6
Factorial of 6 is = 720

Screenshot

Related Posts

Add Two Numbers Flow Chart

Java Programs /

Write a simple java program to add two numbers?

Fahrenheit To Celsius

Java Programs /

Java program to Convert Fahrenheit to Celsius

Java Programs /

Java program for area of triangle

Area of Rectangle

Java Programs /

Write a Java program to calculate Area of Rectangle

Volume of a Cylinder

Java Programs /

Volume of a Cylinder Java Program

‹ Write a Java program to calculate Area of Rectangle› Java program for area of triangle

Recent Posts

  • What are the advantages of PHP?
  • What are the advantages of JavaScript?
  • Taskbar Settings Windows 11How to Hide Taskbar on Windows 11
  • PATH variable Windows 11How to edit PATH variable on Windows 11?
  • TPM on Windows 11Enable BitLocker Drive Encryption on Windows 11
  • Windows 11 Lock Option Start MenuHow to lock Windows 11 PC
  • Add widgets Windows 11How to Add, Remove Widgets on Windows 11
  • Rename PC Windows 11How to Rename PC on Windows 11?
  • Windows 11 Shutdown Login ScreenHow to Shut down Windows 11 PC?
  • Windows 11 FeaturesHow to launch command prompt on Windows 11
  • Windows 10 Take snapshotInstall Windows 11 Insider Preview on Virtual Machine
  • Create a restore pointHow to Create a Restore point in Windows 11
  • How to manually Update new Edge Browser on Windows 10How to manually Update new Edge Browser on Windows 10
  • Updating JDK in IntelliJ IDEAHow to setup JDK 15 environment in IDEs
  • Git Tags How to create a tag on GitHub?

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com