Factorial program in Java without using recursion.


Following is the required program.

Example

Live Demo

public class Tester {
   static int factorial(int n) {
      if (n == 0)
         return 1;
      else
         return (n * factorial(n - 1));
   }
   public static void main(String args[]) {
      int i, fact = 1;
      int number = 5;
      fact = factorial(number);
      System.out.println(number + "! = " + fact);
   }
}

Output

5! = 120

Updated on: 18-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements