Answer to Question #160217 in C++ for Rohan

Question #160217

Write a C++ program that inputs a positive number form user and program will display its factorial. 


1
Expert's answer
2021-01-31T08:01:49-0500
#include <iostream>

int main()
{
    std::cout << "Please enter a positive number: ";
    int number;
    std::cin >> number;

        if(!std::cin || number < 0)
        {
            std::cout << "Bad input\n";
            return 1;
        }

    int factorial = 1;

    if(number > 1)
    {
        for(int i = 1; i <= number; ++i) 
        {
            factorial *= i;
        }
    }

    std::cout << "Factorial is " << 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
New on Blog