Answer to Question #317598 in C++ for Zaki

Question #317598

Write a program that enter a number from user and display its factorial. e.g. factorial of 5 is


1 X 2 X 3 X 4 X 5=120. Where is N<=10. by using do while loop

1
Expert's answer
2022-03-26T02:38:36-0400
#include <iostream>
using namespace std;

int main() {
    int n;
    long int factorial = 1;

    cout << "Enter  integer N (1-10): ";
    cin >> n;

    if (n < 0)
        cout << "Error! Factorial of a negative number doesn't exist";
    else if (n > 10)  
          cout << "Error! N must be <=10";
    else {
        for(int i = 1; i <= n; ++i) {
            factorial *= i;
        }
        cout << "Factorial of " << n << " = " << factorial;    
    }

    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
New on Blog