# Factorial of a Number in C/C++

## Factorial of a Number in C/C++

Factorial of a number is the product of the number and all the numbers below it. For ex factorial of 5 (5!) is 5*4*3*2*1 = 120. The number must be a positive integer. Let’s see a program to find factorial of a number in C/C++.

## 1. Code

### C++ program using while loop

``````//accepting a no and finding its factorial
in C++ using while loop
#include <iostream>

using namespace std;

int main()
{
int num, fact = 1, i;
cout<<"\n Enter a no:";
cin>>num;
i = num;
while(num >= 1)
{
fact = fact * num;
num = num - 1;
}

cout<<"\n Factorial of "<<i<<" is :"<<fact;

return 0;

}
``````

#### using for loop

``````//finding factorial of an accepted no
in C++ using for loop
#include <iostream>

using namespace std;

int main()
{
int num, fact, i;
cout<<"\n Enter a no:";
cin>>num;
i = num;

for(fact=1;num>=1;num--)
{
fact=fact*num;
}

cout<<"\n Factorial of "<<i<<" is :"<<fact;

return 0;

}``````

### C program

``````//finding factorial of an accepted no in C
#include <stdio.h>

int main()
{
int num,fact;
printf("\n Enter a no:");
scanf("%d",&num);

fact=1;

while(num>=1)
{
fact=fact*num;
num--;
}

printf("\n Factorial:%d",fact);

return 0;

}``````

We can use for loop in the similar manner as we used in C++ code.

## 2. Output

Also read : How C++ is different from C.

## 3. Program parts

include is a pre-processor directive. It is used to include header files.
stdio/iostream is header file which has certain commands that c/c++ supports. E.g. return, main, etc.
conio is a header file which is use to include commands like clrscr(),getch() in the code.
.h is an extension for header file.
int is data type. It indicates that the program returns a value.
main() is main function that indicates the compiler that the user written programs starts from here.
num, fact are variable names.
while is looping structure in C/C++. It repeats the code inside it untill a specific condition is satisfied.
for is also a looping structure. It works same as while loop. Only difference is increment/decrement takes places inside te while loop.
print/cout is the keyword use to print a message.
return statement is use to return a value. If any error occurs the program will return 0.

## 4. Explanation

First the program will check the header files. If appropriate header files are present then program will enter the main function. Here it will accept a number of integer data type. Then for/while loop will be initialised and the loop will continue to execute until the specified condition is satisfied. After coming out of loop it will finally print the factorial of entered number.

## Conclusion

So that’s how we write a program to find factorial of a number. Every program has many different logics. Tell us if you have any other method or way to find factorial of a number. Also if you liked this post share with your programming buddies. And tell us what other programs would you like us to post in the comment section below.