Factorial Program in C++ Using While Loop Structure

- March 13, 2018
In mathematics, the factorial is denoted by "Exclamation mark" (!). For example the factorial of a non-negative integer n, will be "n!" that is the product of all positive integers less than or equal to n. Such as:
6! = 6 x 5 x 4 x 3 x 2 x 1 = 720
How to find factorial of any number in cpp

In this example, you will learn to calculate the factorial of a number using C++ while loop. The factorial of 0 is equal to 1 and factorial of negative number doesn't exist.

Factorial Program

#include<iostream>
using namespace std;
main()
 {
   long factorial, num, c;
   cout<<"Enter positive number to find factorial:";
   cin>>num;
   factorial = 1;
   c = 1;
   while (c <= num)
   {
       factorial = factorial * c;
       c =  c + 1;
   }
 cout<<"Factorial of "<<num<<"="<<factorial;
 return 0;
}
The above C++ program takes a positive number from the user, computes its factorial and displays the result.

Program Output:

Enter positive number to find factorial: 6
Factorial of 6 = 720