Solved ProgramsISCClass 12Computer Science

Factorial of a number (Recursive Function)

To learn more about how to make a program in java watch this video https://youtu.be/TRm9r4gFTho

Updated
Solved Programs

The factorial of a number is the product of all the integers from 1 to that number.

Recursion :- The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function. To learn more about recursion click the given link.
Recursion

To learn more about how to make a program in java watch this video https://youtu.be/TRm9r4gFTho

Write a recursive program to print the factorial of a number.

import java.io.*;
public class FactorialRecur
{
    int num;
    void input()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("enter the number");
        num=Integer.parseInt(br.readLine());
    }
    int factorial(int n)
    {
        if(n==0)
        return 1;
        else
        return(n*factorial(n-1));
    }
    void display()
    {
        System.out.println("the factorial of the number is:"+factorial(num));
    }
    public static void main()throws IOException
    {
        FactorialRecur obj=new FactorialRecur();
        obj.input();
        obj.display();
    }
}

Output:
Enter the number
4
The factorial of the number is : 24