All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Factorial Program In Java Using Recursion

Last Updated : Mar 11, 2024

Factorial Program In Java Using Recursion

In this article we will show you the solution of factorial program in java using recursion, first you need to import packages, in case using eclipse then itself that import package 'Nextset'.

We defined main class with the same name of program, which contains fact and static main methods.

In the fact() method we proceeds factorial calculation. Then in main method we printed factorial result on terminal.

Step By Step Guide On Factorial Program In Java Using Recursion :-

Within class 'rec_fact_prgm' we defined recursive fact() method. Generally, recursive will makes your work easy, with the help you can proceed same process with few lines of code.

In main method, you have create object for main class to call fact() method. Here we found factorial for number '8'.

But you can get factorial for any number by dynamic inputs and lastly we printed result.

//recursive factorial program
package Nextset;
class rec_fact_prgm
{
 int fact(int num){
  if(num==0||num==1){
   return 1;
  }
  return num*fact(num-1);
 }
 public static void main(String args[])
    {
        rec_fact_prgm cob=new rec_fact_prgm();
        int res=cob.fact(8);
        System.out.println("8 factorial is "+res);
    }
}
  1. The ‘Package Nextset’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. In main class 'rec_fact_prgm', we provide definition for fact() method. May everyone know returning method have to specify with returning result datatype at front. That's why 'int fact()' gave, there using if we checks whether argument value '1 or 0'. If true then returns value '1' to result otherwise that specific number multiplies with its lesser values again and again.
  3. It reverse orderly multiplies till value '1' and returning final result to function call begin place. In main method, we creating object 'cob' for main calss to call fact() method with default value '8'.
  4. The ‘public static void main(String[] args)’ is defined that's starting point from where compiler starts program execution.
  5. In case, you need to finds out factorial for anyother number then you have to modify that '8' only. You will get result according to your changes made.
  6. At finally we printed result as '8 factorial is 40320'.
  7. The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  8. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know how to calculate factorial for a number in java.

To compile java program you need to tell the java compiler which program to compile through ‘javac rec_fact_prgm.java’.

After successful compilation you need to execute ‘java rec_fact_prgm‘line for gets the output.

Then you can get the result text '8 factorial is 40320' on terminal. On eclipse just press of run button then you will get output inside terminal.

I hope this article on factorial program in java using recursion helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪