Question

Find the factorial of a number using recursion

				
					ENTER THE NUMBER:5
Factorial of 5:120
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
class FactorialOfNumber
{
    public static int Factorial(int num)
    {
        if(num==1)
        {
            return 1;
        }
        else
        {
            return num*Factorial(num-1);
        }
    }

    public static void main(String args[])
    {
        int num=0,fac=1;

        Scanner sc=new Scanner(System.in);
        System.out.print("ENTER THE NUMBER:");

        num=sc.nextInt();
        fac=Factorial(num);
        System.out.println("Factorial of "+num+":"+fac); 

    }

}

				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *