Programming Examples

Java recursive function to find factorial of a number


Write a Java program to create a recursive function for calculating the factorial value of given number.

import java.util.*;
class Factorial
{
    int fact(int n)
    {
       if(n==0)
        return 1;
       else
       return (n*fact(n-1));
    }    
    public static void main(String arr[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter Any Number : ");
        int num=sc.nextInt();
        Factorial obj=new Factorial();
        int ans=obj.fact(num);
        System.out.println("Factorial value of "+num+" is : "+ans);
        
    }
}
Output

Enter Any Number : 

5

Factorial value of 5 is : 120

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz