Answer to Question #188829 in C++ for sss

Question #188829

write a complete c++ program to input an integer n and compute its zfactorial. your program should check that the range of n is 0<n<10 before the computation . if the value of n is not in the range , display an appropriate error message and input the value of n again until the user enter the valid value of n.


1
Expert's answer
2021-05-06T09:25:36-0400
#include <iostream>
using namespace std;


//define a function to return factorial of a number
int factorial(int a)
{
    int fact=1;
    for(int i = 1; i <=a; ++i) {
            fact *= i;
        }
    return fact;
}
int main()
{
    int n;
    while (true){
        cout<<"\nEnter an integer between 0 and 10: ";
        cin>>n;
        if (n>0 && n<10)
            {
                cout << "\nFactorial of " << n << " = " <<factorial(n)<<endl;
                break;
            }
        else{
            cout<<"Invalid input";
        }
    }
    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!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS