Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Question 5! = 5times4times3times2times1 = 120. Write a program that defincs

ID: 3545897 • Letter: C

Question

C++ Question

5! = 5times4times3times2times1 = 120. Write a program that defincs two overloaded versions of the function factorial(), which compute factorial and return the answer (do not use cout statements inside the functions). Your prototypes should be the following: int factorial(int); long long int factorial(long long int); In your main program ask the user to input a positive integer, and then call each function and print the result for each function. Note that the function that is called will depend on the type of your input. You should therefore first cast your user input as follows: Int nun; long long int num_long; cout « "Please enter a positive integer: "; cin » num; num_long - (long long int) num; Your ouput should look as follows: Please enter a positive integer: 3 int factorial: 6 long long factorial: 6_ Please enter a positive integer: -21 Your input is invalid

Explanation / Answer

#include<iostream>
using namespace std;
int factorial(int k)
{
if(k==0) return 1;
else return k*factorial(k-1);
}
long long int factorial(long long int k)
{
if(k==1) return 1;
else return k*factorial(k-1);
}
int main()
{
int num;
long long int num_long;
cout << "Please enter a positive integer: ";
cin >> num;
if(num>=0)
{
    cout << endl;
num_long = (long long int) num;
cout << "int factorial: " << factorial(num) << endl;
cout << "long long factorial: " << factorial(num_long) << endl;
}
else
cout << "Your input is invalid " << endl;
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote