Advertisement
MrPinzon

Factorial.java

Apr 12th, 2022
736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.59 KB | None | 0 0
  1. public class Factorial
  2. {
  3.     public static int getFactorial(int number)
  4.     {
  5.         int result = number; //result is the original value
  6.         if (number>1) //while the number is greater than 1
  7.         {
  8.             result *= getFactorial(number-1); //call the same function (recursion) with a modified argument
  9.         }
  10.         else if (number == 1)
  11.             return 1; //this case avoids an infinite recursion
  12.         return result; // get the actual result
  13.     }
  14.     public static void main(String[] args)
  15.     {
  16.         System.out.println(getFactorial(5)); //test case
  17.     }
  18. }
  19.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement