C++ Program to Find Factoial of a Number using Iteration

«
»

This is a C++ Program to find the factorial of a number using iteration.

Problem Description

We have to write a C++ program to find out the factorial of a given number using iteration. In mathematics, the factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n.
For example,
5! = 5 * 4 * 3 * 2 * 1 = 120
The value of 0! is 1, according to the convention for an empty product.
NOTE: Factorial is calculated only for non negative integers.

Expected Input and Output

Case 1. When we take a positive integer

advertisement
If the input number is 5,
then the expected output will be 120.

Case 2. When the number is zero.

If the input number is 0,
then the expected output will be 1.
Problem Solution

1. In order to find factorial of a number we can either create a separate function or calculate in the main function itself.
2. First of all input the number whose factorial is to be determined (say n).
3. Make a copy the value of n.
4. Take a product variable and initialize it with 1 (say pro).
5. Run a loop from n to 1.
6. Multiply pro by index value.
7. At each iteration the value of index will get decreased by 1 till it becomes equal to 1.
8. This way we calculate n! as n*(n-1)*(n-2)*(n-3)……..1

advertisement
Program/Source Code

Here is source code of the C++ Program to find the factorial of a number using iteration. The program is successfully compiled and tested using Codeblocks gnu/gcc compiler on Windows 10. The program output is also shown below.

  1. /*
  2.  * C++ Program to find factorial of a number using iteration.
  3.  */
  4. #include<iostream>
  5. using namespace std;
  6. class fact
  7. {
  8. public:
  9.     /*
  10.      * Function to find the factorial
  11.      */
  12.     int factorial(int n)
  13.     {
  14.         int i;
  15.         int pro = 1;
  16.         for(i = n; i > 1; i--)
  17.         {
  18.             pro = pro * i;
  19.         }
  20.         if(n == 0)
  21.         {
  22.             pro = 1;
  23.         }
  24.         return pro;
  25.     }
  26. };
  27. /*
  28.  * Main function
  29.  */
  30. int main()
  31. {
  32.     fact f;
  33.     int n;
  34.     cout<<"Enter the number whose factorial is to be calculated\t";
  35.     cin>>n;
  36.     cout<<"\nFactorial of "<<n<<" is = "<<f.factorial(n);
  37.     return 0;
  38. }
Program Explanation

1. Here in this program we have created a function called factorial which takes in the number whose factorial is to be determined.
2. It calculates the factorial and returns the value stored in pro variable.
3. If the value of n is 0 then factorial will be = 0.

advertisement
Runtime Test Cases
1. Enter the number whose factorial is to be calculated    5
 
   Factorial of 5 is = 120
2. Enter the number whose factorial is to be calculated    0
 
   Factorial of 0 is = 1

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn