Write a Java-program to print Factorial of upto N Numbers using while loop ?

Write a Java-program to print Factorial of upto N Numbers using while loop ?

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

Output:

1 Factorial is: 1
2 Factorial is: 2
3 Factorial is: 6
4 Factorial is: 24
5 Factorial is: 120

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

Output:

1 Factorial is: 1
2 Factorial is: 2
3 Factorial is: 6
4 Factorial is: 24
5 Factorial is: 120

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

Output:

Enter n value : 5
1 Factorial is: 1
2 Factorial is: 2
3 Factorial is: 6
4 Factorial is: 24
5 Factorial is: 120





More Questions


65 . Write a Java-program to print Factorial Number using while loop ?
66 . Write a Java-program to print Factorial of upto N Numbers using while loop ?
67 . Write a Java-program to print sum of natural numbers using while loop ?
68 . Write a Java-program to print sum of Even numbers using while loop ?
69 . Write a Java-program to print sum of Odd numbers using while loop ?
70 . Write a Java-program to print sum of even or odd numbers based on the N value using while loop ?
71 . Write a Java-program to print sum of even or odd numbers based on the N value using single while loop ?
72 . Write a Java-program to display the Table using while loop ?
73 . for and while difference program(Factorial Number using while Loop (value must be positive number))
74 . Write a Java-program to display 1 to 10 values using do while loop ?
75 . Write a Java-program to display 10 to 1 values using do while loop