Calculate the Factorial of a Number Using a Loop in C++

This exercise teaches you how to calculate the factorial of a number using an iterative approach in C++. The factorial of a number N is the product of all positive integers less than or equal to N. It is denoted as N! and can be expressed as:

N! = N * (N-1) * (N-2) * ... * 1

For example:
5! = 5 * 4 * 3 * 2 * 1 = 120

In this program, the user will input a positive integer, and the program will calculate and display the factorial of that number using a loop. The loop will multiply the numbers iteratively, starting from 1 up to the given number N.

This is a great exercise for understanding loops, basic arithmetic operations, and user input handling in C++.

Group

Flow Control in C++

Objective

1. Prompt the user to enter a positive integer.
2. Initialize a variable to store the factorial result, starting with 1.
3. Use a loop to multiply the factorial result by each integer from 1 to N.
4. Display the final factorial result.
5. Test the program with different values of N.

Calculate the factorial of a number using a loop.

Example C++ Exercise

 Copy C++ Code
#include <iostream> // Include the iostream library for input and output

using namespace std; // Use the standard namespace

// Main function - the entry point of the program
int main() {
    int N, factorial = 1; // Declare the variable N to store user input and factorial initialized to 1

    // Ask the user to enter a positive integer
    cout << "Enter a positive integer: ";
    cin >> N; // Read the user input and store it in N

    // Use a for loop to calculate the factorial of the number
    for (int i = 1; i <= N; ++i) {
        factorial *= i; // Multiply factorial by each integer from 1 to N
    }

    // Display the result of the factorial
    cout << "The factorial of " << N << " is: " << factorial << endl;

    return 0; // Return 0 to indicate that the program executed successfully
}

 Output

Enter a positive integer: 5
The factorial of 5 is: 120

Share this C++ Exercise


More C++ Programming Exercises of Flow Control in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.

  • Print Numbers from 1 to 100 Using a For Loop in C++

    In this exercise, you will learn how to use a `for` loop in C++ to print numbers from 1 to 100. A `for` loop is commonly used when you know the number of iterations in advance. In ...

  • Print a Triangle of Asterisks in C++

    In this exercise, you will write a C++ program that prints a triangle pattern using asterisks (`*`). This is a common exercise used to practice loops and nested loops. The pattern ...

  • Print Numbers from 10 to 1 in Reverse Order in C++

    In this exercise, you will write a C++ program that prints numbers from 10 to 1 in reverse order. This will help you practice using loops, particularly with decrementing values. By...

  • Simple Calculator Simulation in C++

    In this exercise, you will create a C++ program that simulates a basic calculator. The calculator will perform simple arithmetic operations such as addition, subtraction, multiplic...

  • Find the Largest of Three Numbers in C++

    In this exercise, you will write a C++ program that compares three numbers and determines which one is the largest. The program will use `if-else` statements to compare the three i...

  • Check if a Number is Divisible by 5 and 3 Simultaneously in C++

    In this exercise, you will write a C++ program that checks if a given number is divisible by both 5 and 3 simultaneously. The program will take an integer input from the user and u...