Write a C++ program with a user-defined function myFactorial,
that calculates the
factorial of a number entered by the user. Return the calculated
factorial and print it in
the main function.
#include <iostream> using namespace std; int myFactorial(int n){ if(n<0){ return 0; } else if(n <= 1){ return 1; } return n*myFactorial(n-1); } int main() { int n; cout << "Enter a number: "; cin >> n; cout << n <<"! = " << myFactorial(n) << endl; return 0; }
Get Answers For Free
Most questions answered within 1 hours.