Welcome to Westonci.ca, your go-to destination for finding answers to all your questions. Join our expert community today! Explore in-depth answers to your questions from a knowledgeable community of experts across different fields. Join our Q&A platform to connect with experts dedicated to providing accurate answers to your questions in various fields.

write a c++ program to calculate the factorial of a number

Sagot :

tonb

Answer:

#include <iostream>

int factorial(int n) {

 return (n > 1) ? n*factorial(n-1) : 1;

}

int main() {

 std::cout << factorial(5);

}

Explanation:

Above is an approach using recursion.

It will output 120, which is 5! = 5*4*3*2*1

Thank you for your visit. We're committed to providing you with the best information available. Return anytime for more. We appreciate your time. Please revisit us for more reliable answers to any questions you may have. Thank you for using Westonci.ca. Come back for more in-depth answers to all your queries.