0% found this document useful (0 votes)
108 views2 pages

Factorial

This document discusses two ways to write a factorial program in C++: using a loop and using recursion. It provides examples of the code for each approach. Factorial of a number is the product of all positive descending integers from that number to 1. For example, 5! = 5 * 4 * 3 * 2 * 1 = 120. The code samples demonstrate how to use a for loop or recursive function calls to calculate the factorial of a given number entered by the user.

Uploaded by

Zain Ul Basit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views2 pages

Factorial

This document discusses two ways to write a factorial program in C++: using a loop and using recursion. It provides examples of the code for each approach. Factorial of a number is the product of all positive descending integers from that number to 1. For example, 5! = 5 * 4 * 3 * 2 * 1 = 120. The code samples demonstrate how to use a for loop or recursive function calls to calculate the factorial of a given number entered by the user.

Uploaded by

Zain Ul Basit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

https://www.javatpoint.

com/factorial-program-in-cpp

next →← prev

Factorial program in C++


Factorial Program in C++: Factorial of n is the product of all positive descending
integers. Factorial of n is denoted by n!. For example:

1. 4! = 4*3*2*1 = 24
2. 6! = 6*5*4*3*2*1 = 720

Here, 4! is pronounced as "4 factorial", it is also called "4 bang" or "4 shriek".

The factorial is normally used in Combinations and Permutations (mathematics).

There are many ways to write the factorial program in C++ language. Let's see the 2 ways
to write the factorial program.

o Factorial Program using loop


o Factorial Program using recursion

Factorial Program using Loop


Let's see the factorial Program in C++ using loop.

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int i,fact=1,number;
6. cout<<"Enter any Number: ";
7. cin>>number;
8. for(i=1;i<=number;i++){
9. fact=fact*i;
10. }
11. cout<<"Factorial of " <<number<<" is: "<<fact<<endl;
12. return 0;
13. }

Output:

Enter any Number: 5


Factorial of 5 is: 120
https://www.javatpoint.com/factorial-program-in-cpp

Factorial Program using Recursion


Let's see the factorial program in C++ using recursion.

1. #include<iostream>
2. using namespace std;
3. int main()
4. {
5. int factorial(int);
6. int fact,value;
7. cout<<"Enter any number: ";
8. cin>>value;
9. fact=factorial(value);
10. cout<<"Factorial of a number is: "<<fact<<endl;
11. return 0;
12. }
13. int factorial(int n)
14. {
15. if(n<0)
16. return(-1); /*Wrong value*/
17. if(n==0)
18. return(1); /*Terminating condition*/
19. else
20. {
21. return(n*factorial(n-1));
22. }
23. }

Output:

Enter any number: 6


Factorial of a number is: 720

You might also like