Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Factorial
- {
- public static int getFactorial(int number)
- {
- int result = number; //result is the original value
- if (number>1) //while the number is greater than 1
- {
- result *= getFactorial(number-1); //call the same function (recursion) with a modified argument
- }
- else if (number == 1)
- return 1; //this case avoids an infinite recursion
- return result; // get the actual result
- }
- public static void main(String[] args)
- {
- System.out.println(getFactorial(5)); //test case
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement