Write a C++ program in which find the factorial of a number using function.

Program Code:
#include<iostream>
using namespace std;

// user defined function declaration

int fact(int);


// main() function definition with user defined function call in it

int main() 
{
int n;
cout <<"Enter a number to find the factorial : ";
cin>>n;
cout<<endl;
cout<<"Factorial of the given number is : " ;
// user defined function call fact(n)
cout<< fact(n);
return 0;
}

// user defined function definition which takes argument from the main program

int fact(int n) 
{
int i;
int f = 1;
for (i = n; i >= 1; i--) 
{
f = f * i;
}
return f;
}

The program output is tested on www.jdoodle.com
Output:
Enter a number to find the factorial : 5
Factorial of the given number is : 120


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: