Write a C++ program to find factorial of given number.

Write a C++ Program to Find Factorial of a Given Number

Factorial is one of the most fundamental concepts in mathematics and computer science. It is widely used in permutations, combinations, probability, and algorithm analysis. In this tutorial by cslearning.org, we will learn how to write a simple and student-friendly C++ program to find the factorial of a given number.

This guide includes:

  • Introduction to factorial
  • Logical explanation
  • Easy C++ source code
  • Sample output
  • Viva questions with answers

1. Introduction to Factorial

The factorial of a non-negative integer n is the product of all positive integers less than or equal to n.

It is represented by n!

Formula:

n! = n × (n-1) × (n-2) × ... × 1

Example:

  • 5! = 5 × 4 × 3 × 2 × 1 = 120
  • 4! = 4 × 3 × 2 × 1 = 24
  • 0! = 1 (By definition)

Factorial plays an important role in:

  • Permutations and combinations
  • Probability theory
  • Algorithm complexity analysis
  • Recursion problems

2. Logic of the Program

To calculate the factorial of a number in C++, we follow these steps:

  1. Start the program.
  2. Declare variables.
  3. Take input from the user.
  4. Initialize a variable fact = 1.
  5. Use a loop from 1 to n.
  6. Multiply fact with each number.
  7. Display the result.

Step-by-Step Logic:

  • Suppose the user enters 5.
  • Start with fact = 1.
  • Multiply:
    • fact = 1 × 1 = 1
    • fact = 1 × 2 = 2
    • fact = 2 × 3 = 6
    • fact = 6 × 4 = 24
    • fact = 24 × 5 = 120

Final Answer = 120


3. C++ Source Code (Simple and Student-Friendly)

#include <iostream>
using namespace std;

int main()
{
    int n;
    long long fact = 1;

    cout << "Enter a number: ";
    cin >> n;

    for(int i = 1; i <= n; i++)
    {
        fact = fact * i;
    }

    cout << "Factorial of " << n << " is: " << fact;

    return 0;
}

Explanation of Code:

  • #include <iostream> → Used for input and output.
  • long long fact = 1; → Stores large factorial values.
  • for loop → Repeats multiplication from 1 to n.
  • Output → Displays the factorial.

This program uses basic loop logic, making it perfect for beginners.


4. Sample Output

Example 1:

Enter a number: 5
Factorial of 5 is: 120

Example 2:

Enter a number: 4
Factorial of 4 is: 24

5. Viva Questions with Answers

Q1: What is factorial?

Answer:
Factorial of a number is the product of all positive integers from 1 to that number. It is denoted by n!.


Q2: What is the factorial of 0?

Answer:
The factorial of 0 is 1.


Q3: Which loop is used in this program?

Answer:
A for loop is used to calculate the factorial.


Q4: Why is long long used for factorial?

Answer:
Factorial values grow very fast, so long long is used to store larger numbers.


Q5: Can factorial be calculated using recursion?

Answer:
Yes, factorial can also be calculated using a recursive function in C++.


Conclusion

In this tutorial, we learned how to write a C++ program to find the factorial of a given number using simple loop logic. Understanding factorial is important for many advanced computer science topics like recursion, algorithms, and probability.

Keep practicing more C++ programs on cslearning.org to strengthen your programming fundamentals!

Spread the love

Leave a Comment

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

Scroll to Top