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
Post A Comment:
0 comments: