1. Write function in C++ that will find factorial of a number. Number must be passed from calling function as an argument to function parameters.
/*
C++ that finds factorial of a number.
with number passed from calling function as an argument to function parameters
*/
#include<iostream>
using namespace std;
//Function declaration
int factorial(int num);
int main()
{
int num;
cout<<"Enter a positive integer and press enter to calculate factorial: ";
cin>>num;
cout << "Factorial of " << num << " is: " <<factorial(num);
return 0;
}
int factorial(int x)
{
//Check if it is a positive integer
if(x > 1)
//Calculate factorial using recursion
return x * factorial(x - 1);
else
return 1;
}
Comments
Leave a comment