ComputerShikshak.com

Java - Factorial of a Number

Problem Statement

Write a program in Java to find the Factorial of an integer number entered by the user.

Brief Description

According to the problem statement we have to write a program in Java that will take an integer value as input from the user through the keyboard and will find the factorial of that number.

Factorial of a number is obtained by multiplying all the integer values from 1 to that that number.

Also note the following two important points in order to find the factorial of a number:
(i) Factorial is not defined for the negative numbers.
(ii) Factorial of 0 is 1 i.e. 0!=1.

For example, if the user enters 5 as input, the program will generate 120 as output since 5!=1*2*3*4*5=120.

To view Flowchart click the link given below:

Flowchart – Factorial of a Number

Program

				
					import java.util.*;
class Factorial
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int num,fact=1,i;
        System.out.print("Enter a number: ");
        num=sc.nextInt();
        if(num<0)
            System.out.print("Factorial is not defined for the negative numbers");
        else
        { 
            for(i=1;i<=num;i++)
            {
                fact=fact*i;    //OR fact*=i;
            }
            System.out.print("Factorial of "+num+" is "+fact);
        }
    } 
}
				
			

Output

Enter a number: 5
Factorial of 5 is 120

Explanation

In this program:
  • 3 integer variable namely num, fact and i are declared.
  • Initially 1 is stored in fact.
  • The integer value entered by the user is stored in num.
  • If a negative number is entered by the user, the condition num<0 is evaluated to true and the message “Factorial is not defined for the negative numbers” is displayed.
  • If a number which is greater than or equal to zero is entered, the condition num<0 is evaluated to false and control goes to the else block and the statements written within else block are executed.
The step-by-step working of the for loop (when num contains 5) is shown in the table given below:
numi<=num
fact=fact*i;facti++
OR
i=i+1
iComment
5----------1-----1Initial Values
51<=5

true
fact=1*1;1i=1+12Iteration 1
52<=5

true
fact=1*2;2i=2+13Iteration 2
53<=5

true
fact=2*3;6i=3+14Iteration 3
54<=5

true
fact=6*4;24i=4+15Iteration 4
55<=5

true
fact=24*5;120i=5+16Iteration 5
56<=5

false
--------------------For loop
stops
working
as
condition
is
evaluated
to
false
While executing the for loop:
  • At first 1 is stored in i.
  • The condition i<=num is evaluated. 
  • The condition i<=num will produce either true or false after evaluation. 
    • If it is true, the statement written within for loop is executed and the loop continues to iterate till the condition is true. 
    • If it is false then the loop stops working and the control goes to the statement written immediately after the loop. 
  • At the end of each iteration, the value of i is incremented by 1 by executing the statement i++.
  • Within for loop, the value obtained in i is multiplied with the value stored in fact and the result is stored in the same variable i.e. fact.

Thus the above process generates the Factorial of the given number by multiplying all the integer values from 1 to that number. 

The final result is stored in fact. 

Finally the result is displayed on the output screen.

Share this page on
Scroll to Top