Showing posts with label java factorial program. Show all posts
Showing posts with label java factorial program. Show all posts

Saturday, 17 May 2014

JAVA Program to Find the Factorial of a Number

Factorial of a number USING JAVA

A program to accept a number from keyboard and print the Factorial.


//A small java program to calculate the Factorial
import java.util.Scanner;
public class Factorial
{
public static void main(String[] args)
{           final int fact;
             System.out.println("Enter a number to find the Factorial");
             Scanner in = new Scanner(System.in);
             fact=in.nextInt();
             System.out.println( "Factorial of " + fact + " is " + factorial(fact));
}

public static int factorial(int n)
{ 
             int result = 1;
for(int i = 2; i <= n; i++)
result *= i;
return result;
}

}

Output:


Enter a number to find the Factorial
5
Factorial of 5 is 120