Software Development Exam  >  Software Development Notes  >  Basics of C++  >  Code: Find Factorial of a Number

Code: Find Factorial of a Number | Basics of C++ - Software Development PDF Download

Introduction

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example, the factorial of 5 is 5 x 4 x 3 x 2 x 1 = 120.
In this article, we will explain how to find the factorial of a number using C++ programming language. We will provide you with a detailed explanation of the code, including an example with input, code, and output.

Understanding the Code

The C++ code to find the factorial of a number involves the use of a loop statement. There are two types of loops: for loop and while loop. We will use a for loop to find the factorial of a number.
Here is the code to find the factorial of a number in C++:

#include <iostream>

using namespace std;

int main()

{

    int n, fact = 1;

    cout << "Enter a number: ";

    cin >> n;

    for (int i = 1; i <= n; i++)

    {

        fact *= i;

    }

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

    return 0;

}

Let's break down the code line by line:

  • The first line #include <iostream> is a preprocessor directive that tells the compiler to include the iostream library, which contains input and output operations in C++.
  • using namespace std; is a statement that allows us to use the standard C++ namespace. This is necessary for using standard input and output functions.
  • The main() function is the entry point of the program. It is where the program execution begins.
  • int n, fact = 1; declares two integer variables n and fact and initializes fact to 1.
  • cout << "Enter a number: "; outputs the message "Enter a number: " to the console.
  • cin >> n; reads the input from the console and stores it in the variable n.
  • The for loop is used to calculate the factorial of n. The loop starts with i being initialized to 1 and continues until i is less than or equal to n. The loop increments i by 1 at each iteration.
  • fact *= i; multiplies the current value of fact by the value of i at each iteration of the loop.
  • cout << "Factorial of " << n << " is " << fact; outputs the message "Factorial of n is fact" to the console.
  • return 0; ends the execution of the program and returns the value 0 to the operating system.

Input and Output

Let's see an example of how to use this code to find the factorial of a number.

Example: Suppose we want to find the factorial of 5. We would enter the number 5 as the input to the program.
The output of the program would be:

Enter a number: 5

Factorial of 5 is 120

Explanation: The program prompts the user to enter a number. The user enters 5. The for loop multiplies fact by 1, 2, 3, 4, and 5, and stores the result in fact. The value of fact after the loop is 120. Finally, the program outputs the message "Factorial of 5 is 120" to the console.

Conclusion

In this article, we have explained how to find the factorial of a number using C++ programming language. We provided you with a detailed explanation of the code, including an example with input, code, and output. We hope this article has helped you to understand how to find the factorial of a number using C++. Happy coding!

The document Code: Find Factorial of a Number | Basics of C++ - Software Development is a part of the Software Development Course Basics of C++.
All you need of Software Development at this link: Software Development
70 videos|45 docs|15 tests

Top Courses for Software Development

70 videos|45 docs|15 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Semester Notes

,

Previous Year Questions with Solutions

,

past year papers

,

Important questions

,

mock tests for examination

,

Code: Find Factorial of a Number | Basics of C++ - Software Development

,

Summary

,

Code: Find Factorial of a Number | Basics of C++ - Software Development

,

ppt

,

Extra Questions

,

Free

,

Code: Find Factorial of a Number | Basics of C++ - Software Development

,

study material

,

pdf

,

practice quizzes

,

MCQs

,

video lectures

,

Exam

,

Objective type Questions

,

Viva Questions

,

shortcuts and tricks

,

Sample Paper

;