Write a Java program to print Factorial Number Using do While Loop ?

Write a Java program to print Factorial Number Using do While Loop


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

Output:

5 Factorial is 120




Using commandLine arguments

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

Output:

5 Factorial is 120




Using Scanner class

import java.util.*;
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;  
           x++;
        }while(x<=n);
       System.out.print(n+" Factorial is "+fact);
}
}
                                            

Output:

Enter n value 5
5 Factorial is 120







More Programs


80 . Factorial Number using do while Loop
81 . Factorial of N Numbers using do while Loop
82 . Sum Of Natural Numbers using do while Loop
83 . Sum Of Even Numbers using do while Loop
84 . Sum of Odd Numbers using do while Loop
85 . Sum Of Even Or Odd Numbers Based On The N value using do while Loop
86 . Sum Of Even Or Odd Numbers Based On The N value using single do while Loop
87 . Display Even Numbers Using Continue Statement
88 . Display Vowels Using Continue Statement
89 . Display the whose values divisible by 2 and 3 Continue Statement in java
90 . Break the loop which number is divisible by 2 and 3 in java