Answer to Question #318701 in C++ for Third two

Question #318701

Input a random positive integer.


Using a for() loop, generate the factorial value of the integer and print out the result.Tip: Factorials work like this: Factorial of 5 = 1*2*3*4*5


Note that there is a special case and that is the factorial of 0 which is 1.

1
Expert's answer
2022-03-26T15:29:44-0400
#include <iostream>


int facorial(int n) {
    int res = 1;
    for (int i=2; i<=n; i++) {
        res *= i;
    }
    return res;
}


int main() {
    int n;


   std:: cout << "Enter a positive integer: ";
    std::cin >> n;
    std::cout << "Factorial of " << n << " is " << facorial(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
APPROVED BY CLIENTS