Answer:
The solution code is written in C++:
- #include <iostream>
- using namespace std;
- int factorial(int n){
- if(n == 1){
- return n;
- }else{
- return n * factorial(n-1);
- }
- }
- int main()
- {
- int n;
- cout<<"Please enter a number <= 7 :";
- cin>>n;
-
- if(n <= 7){
- cout<<"Factorial is "<< factorial(n);
- }else{
- cout<<"Wrong input number.";
- }
- return 0;
- }
Step-by-step explanation:
Firstly, create a function factorial with one input parameter (Line 1).
Next, create an if-else statement to check if input n is 1, just return the end (Line 6-7).
Otherwise, the function should return n multiplied by output of the recursive calling of the function itself (Line 8-9).
In the main program, create variable and prompt user to input number (Line 15 -17).
Next, do the input validation checking to ensure the input n must be equal or less than 7 then only call factorial function to calculate the factorial of n and display the answer (Line 19 - 20). Otherwise, the program shall just display error message.