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.
#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;
}
Comments
Leave a comment