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

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

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

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]);
        do
        {
            fact=fact*x;
            System.out.println(x+" Factorial is "+fact);
            x++;
        }while(x<=n);
        
    }
}	

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();
        do
        {
            fact=fact*x;
            System.out.println(x+" Factorial is "+fact);
            x++;
        }while(x<=n);
        
    }
}	

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


82 . Write a Java-program to print Factorial of upto N Numbers using do while loop ?
83 . Write a Java-program to print sum of natural numbers using do while loop ?
84 . Write a Java-program to print sum of Even numbers using do while loop ?
85 . Write a Java-program to print sum of Odd numbers using do while loop ?
86 . Write a Java-program to print sum of even or odd numbers based on the N value using do while loop ?
87 . Write a Java-program to print sum of even or odd numbers based on the N value using single do while loop ?
88 . Write a Java-program to display the Table using do while loop ?
89 . Write a Java-program to display vowels using continue statement ?
90 . Write a Java-program to display the whose values divisible by 2 and 3 Continue statement in java ?
91 . Write a Java-program to Break the loop which number is divisible by 2 and 3 in java ?
92 . Write a Java-program to print a to z character