Java Factorial Example /*   Java Factorial Example   This Java Factorial Example shows how to calculate factorial of ...

Java Factorial Example

00:00 PASSOVE INCOME 0 Comments

Java Factorial Example

  1. /*
  2.   Java Factorial Example
  3.   This Java Factorial Example shows how to calculate factorial of
  4.   a given number using Java.
  5. */
  6. public class NumberFactorial {
  7.         public static void main(String[] args) {
  8.                
  9.                 int number = 5;
  10.                
  11.                 /*
  12.                  * Factorial of any number is !n.
  13.                  * For example, factorial of 4 is 4*3*2*1.
  14.                 */
  15.                
  16.                 int factorial = number;
  17.                
  18.                 for(int i =(number - 1); i > 1; i--)
  19.                 {
  20.                         factorial = factorial * i;
  21.                 }
  22.        
  23.                 System.out.println("Factorial of a number is " + factorial);
  24.         }
  25. }
  26. /*
  27. Output of the Factorial program would be
  28. Factorial of a number is 120
  29. */

0 comments: