×

Factorial of a Number in C++ using while Loop

What is a factorial?

The factorial of a number is the product of all the positive numbers less than or equal to n, indicated by n!

According to the standard for an empty product, the value of 0! is 1.

As an example,

1. 11! = 11 * 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = 39916800
2. 7! = 7 * 6 * 5 * 4 * 3 * 2 * 1 = 5040

Program Code:

This computer code demonstrates how to get the factorial of an integer using a while loop in the C++ programming language.

#include <iostream>
using namespace std;
int main()
{
    	/* variables declaration */
    	int num_1, fact_1 = 1;
	int n = 1;
	cout << "Enter an integer to calculate the factorial of:" << endl;
   	cin >> num_1;
    	/* finding the factorial using the while loop */
    	while(n <= num_1)
    	{
        		fact_1 = fact_1 * n;
        		n ++;
    	}
	cout << "Factorial of the given integer is : " << fact_1 << endl;
	return 0;
}

Output:

Factorial of a Number in C++ using while Loop

What is the process of the factorial method?

1. First, the machine reads the integer to determine its factorial from the user.

2. The value of "n" is then multiplied by the value of "fact_1" in a while loop.

3. The while loop runs until the value of "n" is less than or equal to "num_1." Finally, the provided number's factorial value is displayed.

The above code example is implemented in the following steps:

1. Let us suppose the user typed the number 9.

2. It assigns the values num_1 = 6, n = 1, and fact_1 = 1.

3.  The loop then continues until the while loop's condition is met.

          3.1 While loop condition is true, n <= num_1 (1 < 9 )

                    fact_1 = fact_1 * n  (fact_1 = 1 * 1)  So fact_1 = 1

                    n++                          (n = n+1)            So n = 2

          3.2 While loop condition is true, n <= num_1 (2 < 9 )

                    fact_1 = fact_1 * n  (fact_1 = 1 * 2)  So fact_1 = 2

                    n++                          (n = n+1)            So n = 3

          3.3 While loop condition is true, n <= num_1 (3 < 9 )

                    fact_1 = fact_1 * n  (fact_1 = 2 * 3)  So fact_1 = 6

                    n++                          (n = n+1)            So n = 4

          3.4 While loop condition is true, n <= num_1 (4 < 9 )

                    fact_1 = fact_1 * n  (fact_1 = 6 * 4)  So fact_1 = 24

                    n++                          (n = n+1)            So n = 2

          3.5 While loop condition is true, n <= num_1 (5 < 9 )

                    fact_1 = fact_1 * n  (fact_1 = 24 * 5)  So fact_1 = 120

                    n++                          (n = n+1)            So n = 6

          3.6 While loop condition is true, n <= num_1 (6 < 9 )

                    fact_1 = fact_1 * n  (fact_1 = 120 * 6)  So fact_1 = 720

                    n++                          (n = n+1)            So n = 7

          3.7 While loop condition is true, n <= num_1 (7 < 9 )

                    fact_1 = fact_1 * n  (fact_1 = 720 * 7)  So fact_1 = 5040

                    n++                          (n = n+1)            So n = 8

          3.8 While loop condition is true, n <= num_1 (8 < 9 )

                    fact_1 = fact_1 * n  (fact_1 = 5040 * 8)  So fact_1 = 40320

                    n++                          (n = n+1)            So n = 9

          3.9 While loop condition is true, n <= num_1 (9 <= 9 )

                    fact_1 = fact_1 * n  (fact_1 = 40320 * 9)  So fact_1 = 362880

                    n++                          (n = n+1)            So n = 10

          3.10 While loop condition is false, n>num_1 (10 > 9)

                    It is exiting the while loop.

4. Finally, it produces the output seen below

 The Factorial of 9 is 362880

5. As a result, the program code execution is complete.

Pseudocode

Pseudocode for the number's factorial

INPUT num_1
SET fact_1 = 1, n = 1
WHILE n <= num_1 DO
     	COMPUTE fact_1 = fact_1 * n
	INCREASE n by 1
END LOOP
PRINT fact_1

Algorithm for computing the factorial of a number

Step 1: Start

Step 2: Read the user's input number.

Step 3: Declare and set the variables "fact_1 = 1" and "n = 1."

Step 4: Continue the loop until n <= num_1.

Step 5: Update fact_1 = fact_1 * n

Step 6: Increase the n value by 1 (iterate)

Step 7: Check if Step 4 is true continue else go to Step 9

Step 8: To obtain the factorial of a given integer, print fact_1.

Step 9: Stop


Related Topics

Free vs delete() in C++

Free vs delete() in C++ In this section, we will learn about the free() function and also create a C ++ program of the delete operator. What is free() Function in C++? In...

4 minutes read.

Implementing the sets without C++ STL containers

Many practical features and tools in C++ support us in programming competitions. One of these parts is a set from the Standard Template Library (STL), which offers an effective way...

6 minutes read.

Processing strings using std string stream in C++

A string class object called std: string stream is used to streamline into various variables, just as files can pour into strings. This class's things make use of a string...

3 minutes read.

C++ Global Variable

In C++, a global variable is a variable that is defined outside of any function or class and can be accessed by any function or class in the program. Global...

4 minutes read.

Virtual Functions and Runtime Polymorphism in C++

In this tutorial, we will explore more on virtual functions and runtime polymorphism in the most useful language C++. A virtual function is a member function with the keyword virtual used...

6 minutes read.

C++ Prime number program

In this lesson, you'll learn how to verify whether a given number is a prime number or not in C++, and you'll obtain code to do it. What is the definition...

3 minutes read.

sort() function in C++

This tutorial covers the various built-in sort functions found in the C++ algorithm’s library.  What Does C++ Sort Mean? The concept of sorting in C++ entails rearranging an array's elements in a...

3 minutes read.

Function Pointer in C++

Definition: A function pointer in C++ is the same as a usual pointer which is used to point some variables. Function pointers are used to point functions or say store the...

4 minutes read.

How to Use Getline in C++

What is getline in C++? Similar to the cin function, which allows the programmer to take input from the user getline() function also takes input from the user. But in this...

4 minutes read.

Is it fine to write void main() or main() in C/C++?

In C programming language: The default function return type in the C programming language is "int," which implies that main() will always return an integer value. In C, the void main()...

3 minutes read.

How to create a table in C++

C++ is a programming language that has evolved as an improvement of c language. It includes an object-oriented archetype. C++ programming language is a compiled language that complies with the...

3 minutes read.

Difference between "int main( ) and int main(void)" in C/C++

int main( ) function In the C/C++ programming language, int main() indicates a function that returns an integer at the end of the program execution. In general, a value of '0' indicates...

3 minutes read.

C++ Data Abstraction

Object-oriented programming (OOP) provides a number of characteristics that enable programmers to design programmes based on a variety of ideas, reducing errors and increasing program flexibility. The abstraction of data...

7 minutes read.

C++ if-else

C++ if else Control Statement if else control statement in C++ is used to control the program flow in a two-way direction. When condition returns a true value, then the program executes if condition block otherwise...

1 minute read.

C++ If

C++ Control Statement C++ control statement or decision-making statement is used to control the flow of program statement according to condition applied. C++ if Control Statement An if control statement in C++ is used to...

2 minutes read.

C++ Continue in for loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the start...

4 minutes read.

C++ operator

In this article, we will discuss about the operators in C++ with their types and examples. An operator is specially a symbol that tells compiler to perform specific manipulation. C++ contains...

5 minutes read.

C++ Program to Print Fibonacci Triangle

Fibonacci Triangle Program in CPP Definition: Fibonacci Triangle as the name suggests is the same as the Fibonacci number series where the next element is the sum of the previous two elements....

3 minutes read.

List back () function in C++ STL

The list::back () function of the C++ STL returns a direct reference to the last element in the list container. This function varies from list::end (), which just returns an...

2 minutes read.

C++ Friend Functions

In C++, friends are special functions that are not a part of a class yet have access to its private and protected members. The friend keyword is used to declare...

4 minutes read.