Java program to find the factorial of a given number
KW.java
import java.util.Scanner;
class KW
{
    public static void main(String args[])
    {
        System.out.println("———————————————————————————————————————————");
        System.out.println("Program to find the factorial of a given number");
        System.out.println("———————————————————————————————————————————");
        long i,fact=1,n;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the number ");
        n=sc.nextInt();
        if(n>20||n<0)
        {
            System.out.println("OOP's can't find "+n+"!");
        }
        else
        {
            for(i=1;i<=n;i++)
            {
                fact=fact*i;
            }
            System.out.println(n+"! = "+fact);
        }
        System.out.println("———————————————————————————————————————————");
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW ——————————————————————————————————————————— Program to find the factorial of a given number ——————————————————————————————————————————— Enter the number 20 20! = 2432902008176640000 ——————————————————————————————————————————— kodingwindow@kw:~$ java KW ——————————————————————————————————————————— Program to find the factorial of a given number ——————————————————————————————————————————— Enter the number 50 OOP's can't find 50! ——————————————————————————————————————————— kodingwindow@kw:~$
What Next?
Java Strings
Advertisement