EmSAT Achieve Exam  >  EmSAT Achieve Notes  >  C++ for EmSAT Achieve  >  Code: Find Factorial of a Number

Code: Find Factorial of a Number | C++ for EmSAT Achieve 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 | C++ for EmSAT Achieve is a part of the EmSAT Achieve Course C++ for EmSAT Achieve.
All you need of EmSAT Achieve at this link: EmSAT Achieve
70 videos|45 docs|15 tests

Top Courses for EmSAT Achieve

70 videos|45 docs|15 tests
Download as PDF
Explore Courses for EmSAT Achieve exam

Top Courses for EmSAT Achieve

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

MCQs

,

Code: Find Factorial of a Number | C++ for EmSAT Achieve

,

practice quizzes

,

pdf

,

Code: Find Factorial of a Number | C++ for EmSAT Achieve

,

Exam

,

Important questions

,

study material

,

Free

,

Summary

,

Semester Notes

,

Objective type Questions

,

video lectures

,

Sample Paper

,

Viva Questions

,

Previous Year Questions with Solutions

,

shortcuts and tricks

,

Code: Find Factorial of a Number | C++ for EmSAT Achieve

,

mock tests for examination

,

ppt

,

past year papers

,

Extra Questions

;