Question #268122

Create C++ full program that calculates a factorial (!) of any number between 3 and 9; the

program should give feedback to the user if the number is less than 3(error number is less than3), the program should also give feedback if the number is greater than 9; you can use either the

case or else statement also while loop if applicable


Expert's answer

#include <iostream>
using namespace std;
void factorial(int x){
    if(x < 3){
        cout<<"error number is less than 3\n";
        return;
    }
    if(x > 9){
        cout<<"error number is greater than 9\n";
        return;
    }
    int result = 1;
    int i = 1;
    while(i <= x){
        result *= i;
        i++;
    }
    cout<<"Factorial of "<<x<<" is "<<result<<endl;
}
int main(){
    int number;
    cout<<"Input number: ";
    cin>>number;


    factorial(number);


    return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS