Description
Write a program to find factorial of a given number is a common java program asked in any interview to freshers.The factorial of a non-negative integer n is the product of all positive integers less than or equal to n. For example factorial of 4 is 4*3*2 = 24.There are 2 ways to find a factorial of a given number - One by using for loop and the other using recursion.Following java program uses for loop to find factorial of a given number.
Factorial.javapackage com.devglan; public class Factorial { public long findFactorial(int num){ if(num < 1){ System.out.println("Please provide non-negative number."); } long factorial = 1; for (long i = num; i > 1; i--) { factorial = factorial * i; } return factorial; } public static void main(String [] args){ Factorial factorial = new Factorial(); int num = 34; System.out.println("Factorial of " + num + " is : " + factorial.findFactorial(num)); } }