Write a Java-program to print Factorial Number using for loop ?

Write a Java-program to print Factorial Number using for loop ?

Program
public class Main
{
    public static void main(String args[])
    {
        int x,n=5,fact=1;
        for(x=1; x<=n; x++)
        {  
            fact=fact*x;   
        }
        System.out.print(n+" Factorial is "+fact);
    }
}	

Output:

120

Using CommandLine Arguments
public class Main
{
    public static void main(String args[])
    {
        int x,n,fact=1;
        n=Integer.parseInt(args[0]);
        for(x=1; x<=n; x++)
        {  
            fact=fact*x;   
        }
        System.out.print(n+" Factorial is "+fact);
    }
}	

Output:

120

Using Scanner Class
import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        int x,n,fact=1;
        Scanner s=new Scanner(System.in);
        System.out.println("Enter n value: ");
        n=s.nextInt();
        for(x=1; x<=n; x++)
        {  
            fact=fact*x;   
        }
        System.out.print(n+" Factorial is "+fact);
    }
}	

Output:

Enter n value : 5
120





More Questions


49 . Write a Java-program count the number of divisible numbers in a given range(1-100) without loop
50 . Write a Java-program to print Factorial Number using for loop ?
51 . Write a Java-program to print Factorial of upto N Numbers using for loop ?
52 . Write a Java-program to print sum of natural numbers using for loop ?
53 . Write a Java-program to print sum of Even numbers using for loop ?
54 . Write a Java-program to print sum of Odd numbers using for loop ?
55 . Write a Java-program to print sum of even or odd numbers based on the N value using for loop ?
56 . Write a Java-program to print sum of even or odd numbers based on the N value using single for loop ?
57 . Write a Java-program to display the Table using for loop ?
58 . Write a Java-program to display 1 to 10 values using while loop ?
59 . Write a Java-program to display 10 to 1 values using while loop