Answer to Question #191171 in C++ for python

Question #191171

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.



1
Expert's answer
2021-05-14T23:07:03-0400
/*
    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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment