Factorial Number

Java program to display Factorial of Number using for, while, do-while, method, recursion and without user interaction.

There are different ways to display Factorial of Number is Palindrome or Not in Java.
  1.          Factorial of Number using For Loop
  2.          Factorial of Number using While Loop
  3.          Factorial of Number using Do-While Loop
  4.          Factorial of Number using Method
  5.          Factorial of Number using Recursion
  6.          Factorial of Number without User Interaction

import java.util.*;
public class Factotial 
{
	public static void main(String[] args) 
	{
		Scanner s = new Scanner(System.in);
		System.out.println("\n Enter Number...");
		int number = s.nextInt();
		int fact=1;
			for(int i=1;i<=number;i++)
			{
				fact=fact*i;
			}
	System.out.println("\n Factorial of "+number+" is = "+fact);
	}
}

Output:

Enter Number...
3

 Factorial of 3 is = 6
 
import java.util.*;
public class FactorialWhile 
{
	public static void main(String[] args) 
	{
		Scanner s = new Scanner(System.in);
		System.out.println("\n Enter Number...");
		int number = s.nextInt();
		int fact=1,i=1;
			while(i<=number)
			{
				fact=fact*i;
				i++;
			}
	System.out.println("\n Factorial of "+number+" is = "+fact);
	}
}

Output:
Enter Number...
7

 Factorial of 7 is = 5040
 
import java.util.*;
public class FactDoWhile 
{
	public static void main(String[] args) 
	{
		Scanner s = new Scanner(System.in);
		System.out.println("\n Enter Number...");
		int number = s.nextInt();
		int fact=1,i=1;
			do
			{
				fact=fact*i;
				i++;
			}while(i<=number);
	System.out.println("\n Factorial of "+number+" is = "+fact);
	}
}

Output:

Enter Number...
6

 Factorial of 6 is = 720
 
 
import java.util.*;
public class FactMethod 
{
	void fact(int n)
	{
		int fact=1;
		for(int i=1;i<=n;i++)
		{
			fact=fact*i;
		}
	System.out.println("\n Factorial of "+n+" is = "+fact);
	}
	public static void main(String[] args) 
	{
		Scanner s = new Scanner(System.in);
		System.out.println("\n Enter Number...");
		int number = s.nextInt();
		FactMethod obj = new FactMethod();
		obj.fact(number);
	}
}

Output:

Enter Number...
5

 Factorial of 5 is = 120
import java.util.*;
public class FactorialRecursion 
{
	int fact(int n)
	{
		if(n==0)
			return 1;
		else
			return(n*fact(n-1));
	}
	public static void main(String[] args) 
	{
		FactorialRecursion obj = new FactorialRecursion();
		Scanner s = new Scanner(System.in);
		System.out.println("\n Enter Number...");
		int number = s.nextInt();
		int fact=1;
		fact = obj.fact(number);
	System.out.println("\n Factorial of "+number+" is = "+fact);
	}
}

Output:

Enter Number...
4

 Factorial of 4 is = 24
public class FactorialWithoutUserInteraction 
{
	public static void main(String[] args) 
	{
		int fact=1;
			for(int i=1;i<=8;i++)
			{
				fact=fact*i;
			}
	System.out.println("\n Factorial of 8 is = "+fact);
	}
}

Output:

Factorial of 8 is = 40320

No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32