write c++ program that reads a non-negative integer value, computes the factorial and print the result
#include <iostream>
using namespace std;
int factorial(int n){
int result = 1;
if(n == 1) return 1;
if(n == 2) return 2;
else return n * factorial(n - 1);
}
int main(){
int n;
do{
cout<<"Input a positive integer: ";
cin>>n;
}while(n < 1);
cout<<n<<"! = "<<factorial(n);
return 0;
}
Comments
Leave a comment