# C++ | Factorial of number using function

Note:- If you are looking for simple program of factorial(not using function), please see this.

C++ | Factorial of number.

else continue here……

Introduction

The program is to find the factorial of number using function. The simple program(not using function) link is above. The program using function is given below. The program is not extendable. Go enjoy the program. Lets begin…..

Program to find the factorial of number using function

``````
#include<iostream.h>
#include<conio.h>
//function prototype
int fact(int);
//main function
void main()
{
//clear the screen.
clrscr();
//declare variables as int.
int no,fac;
//Input the number and save it in 'no'
cout<<"Enter the no."<<endl;
cin>>no;
//calculate factorial using function.
fac=fact(no);
//print factorial
cout<<"Factorial of "<<no<<" is "<<fac;
//get character
getch();
}

//function
int fact(int no)
{
int i,f=1;
for(i=1;i<=no;i++)
f=f*i;
return(f);
}
``````

Output

Enter the no.
5
Factorial of 5 is 120

How does it work

1. You enter the number.
2. The function calculates the factorial.
3. The factorial is printed.

Extending it

The program cannot be extended.

Explanation.

1. Include ‘iostream.h’ and ‘conio.h’ files.
2. Declare the function prototype.
4. Start program by first clearing the screen.
5. Declare the variables as int (name them as you want.)
6. Add the cout and cin of number.
7. Call the function and save the factorial in variable.
8. Add the cout of factorial.
9. Add the function definition outside the void main().

At the end

You learnt creating the C++ program of Factorial using function. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Sharing options: