×

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

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.

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.

Dynamic Constructor in C++

Dynamic constructors create dynamic memory by using a dynamic memory allocator new within the constructor. This allows us to initialize objects dynamically. The term dynamic constructor is used when memory...

2 minutes read.

Inheritance and Friendship in C++

In this tutorial, we will look into what Inheritance and Friendship in C++ are, as well as the differences between the two. What is Inheritance in C++: In C++, inheritance is an...

2 minutes read.

Inheritance Program in C++

What is Inheritance? Inheritance is the ability of a class to inherit traits and properties from another class. One of the most crucial aspects of Object-Oriented Programming is inheritance. The ability or...

9 minutes read.

C++ Enumeration

C++ Enumeration In C++, Enum is a special data type that contains some fixed sets of components that have various applications in the programming. Enum works fine with fixed constant sets...

3 minutes read.

DES in C++

The popularity of the Data Encryption Standard (DES) is slightly declining as a result of the discovery that DES is susceptible to very strong attacks. Since DES is a block cypher,...

3 minutes read.

Continue in C++ While 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...

4 minutes read.

Types of polymorphism in C++

What is polymorphism in C++ ? Polymorphism literally translates to "multiple forms". This indicates that the same thing behaves differently depending on the context in programming. Polymorphism is a feature of C++...

6 minutes read.

Top best IDEs for C/C++ Developers in 2024

Nothing in the current digital world is conceivable without programming. Everything needs programming, from the cell phones in our pockets to self-driving cars. Programming is also necessary for the mouse...

9 minutes read.

C++ Break

In this article, we will discuss the C++ Break statement with its syntax, algorithm, pseudocode, and examples. The C++ break statement also terminates the currently active loop or switch statement immediately....

4 minutes read.

C++ Continue

In C++, the continue statement is a useful tool for avoiding specific scenarios without breaking the loop. It is employed inside loops to move directly to the following iteration and...

4 minutes read.

C++ User Defined Exceptions

Overriding and inheriting exception class capabilities may be used to define the new exception. Exception handling can also be used with classes. We may also make an exception for user-defined...

4 minutes read.

C++ Goto

In this article, we will discuss the C++ goto statement with its syntax, use, key features, key points, pseudo code, and examples. What is the goto statement in C++? In C++, the...

4 minutes read.

Ways to Copy a Vector in C++

Vectors in C++ are the same as arrays, along with additional outstanding features than them, like array lists in Java programming language. In Vectors, the size constraint is eliminated, which...

5 minutes read.

System() function in C++

As a part of the c/c+ standard library, the system() function passes commands to be executed by the operating system’s command processor or terminal and returns the completed command. We...

2 minutes read.

Random Number Generator in C++

In programming, we need to frequently create the randomly. For example, a dice game, handing out cards to players, apps for rearranging tunes, etc. T There are two tools available in...

4 minutes read.

C++ Switch

In C++, an expression or variable can be tested against a range of constant values using a switch statement, which is a control flow statement. It offers a productive method...

4 minutes read.

Inheritance in C++ vs Java

Just like we inherit traits from our parents, object-oriented programming has a concept called inheritance. In terms of object-oriented programming, a class's traits and behaviours, or its data and methods,...

4 minutes read.

C++ Socket Programming

In this world, computer networking has become very important for sharing of data. Every good programmer has some knowledge about computer networking. Socket programming is one of the critical topics...

6 minutes read.