Factorial Using Loop Example Program In C++
On this page (6sections)
About this program
This is an example program in c common example program. Read the concept first: C++ Variables and Data Types, then study the code and output below.
Definition
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,
6! = 6 x 5 x 4 x 3 x 2 x 1 = 720
The value of 0! is 1, according to the convention for an empty product.
For Loop Syntax
//common
for (initialization; condition; increment/decrement)
{
}
//for Factorial
for (int counter = 1; counter <= n; counter++) {
fact = fact * counter;
}
Factorial Using Loop Example Program
/* Example Program For Factorial Value Using For Loop In C++
little drops @ thiyagaraaj.com
Coded By: THIYAGARAAJ MP */
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
// Variable Declaration
int counter, n, fact = 1;
// Get Input Value
cout<<"Enter the Number :";
cin>>n;
//for Loop Block
for (int counter = 1; counter <= n; counter++)
{
fact = fact * counter;
}
cout<<n<<" Factorial Value Is "<<fact;
// Wait For Output Screen
getch();
return 0;
}
Sample Output
Enter the Number :6
6 Factorial Value Is 720
Related Pages
Learn the concept first, then study the code:
- C++ Programs — Browse all C++ Programs.
- C++ Variables and Data Types — Tutorial — data types and variable declaration.
- Factorial Using Function Example Program In C++ — More in c common example program.
- Factorial Using Recursion Example Program In C++ — More in c common example program.