Answer to Question #188828 in C++ for akim

Question #188828

write c++ program that reads a non-negative integer value, computes the factorial and print the result


1
Expert's answer
2021-05-05T23:59:28-0400
#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;
}

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