Factorial Calculation in C++

 

Introduction

In mathematics, the factorial of a non-negative integer n is the product of all positive integers less than or equal to n. It is denoted as n! and is defined as:

n! = n × (n – 1) × (n – 2) × … × 1

The factorial function is commonly used in combinatorics, algebra, and calculus, among other areas of mathematics. In this program, we will write a C++ program to calculate the factorial of a given number.

Objective

The objective of this program is to calculate the factorial of a given non-negative integer using C++. The program will take user input for the number and then compute its factorial by multiplying the integers from 1 to that number.

Code

#include 
using namespace std;

// Function to calculate the factorial of a number
long long factorial(int n) {
    long long result = 1;  // Initialize result as 1
    for (int i = 1; i <= n; i++) {
        result *= i;  // Multiply result by current number
    }
    return result;  // Return the computed factorial
}

int main() {
    int num;  // Variable to store user input
    cout << "Enter a non-negative integer: "; cin >> num;  // Input number from user

    // Check if the input is a valid non-negative integer
    if (num < 0) {
        cout << "Please enter a non-negative integer." << endl;
    } else {
        // Calculate factorial and display the result
        cout << "The factorial of " << num << " is " << factorial(num) << endl;
    }
    return 0;
}

Explanation of the Program Structure

The program consists of the following key components:

  • Function Declaration (factorial): This function accepts an integer n and calculates its factorial using a for loop. The result is stored in a long long variable to accommodate larger numbers as factorial values grow rapidly.
  • Input Handling: The main function prompts the user to input a non-negative integer. It then checks whether the input is valid (non-negative). If the input is valid, it calls the factorial function and displays the result.
  • Error Handling: If the user enters a negative integer, the program prompts the user to enter a valid non-negative integer.
  • Output: The program outputs the factorial of the entered number to the console.

How to Run the Program

To run this C++ program on your system, follow these steps:

  1. Write the code: Copy the code provided above and save it in a text file with the extension .cpp, for example, factorial.cpp.
  2. Compile the code: Use a C++ compiler like g++ to compile the program. Open a terminal or command prompt, navigate to the directory where your factorial.cpp file is saved, and run the following command:
    g++ -o factorial factorial.cpp
  3. Run the compiled program: After compilation, execute the program by running the following command:
    ./factorial
  4. Enter input: The program will prompt you to enter a non-negative integer. After entering the number, it will display the factorial of that number.

Example Output

Enter a non-negative integer: 5
The factorial of 5 is 120

 

One Reply to “Factorial Calculation in C++”

Leave a Reply to binance konta izveide Cancel reply

Your email address will not be published. Required fields are marked *