Question #38845

write a c++ program which calculates a factorial of a number using a recursive function
1

Expert's answer

2014-02-04T13:23:40-0500
/* Answer on Question #38845 - Programming - C++ */
#include <iostream>
using namespace std;
unsigned long long factorial(unsigned int number)
{
    return (number < 2) ? 1 : number * factorial(number - 1);
}
int main()
{
    int number;
    cout << "Enter a non-negative number (up to 20): \n";
    if (!(cin >> number) || number < 0 || number > 20)
    {
        cout << "Invalid number.\n";
        return -1;
    }
    cout << "The factorial is: \n" << factorial((unsigned int)number);
    return 0;
}

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!
LATEST TUTORIALS
APPROVED BY CLIENTS