Explore Westonci.ca, the leading Q&A site where experts provide accurate and helpful answers to all your questions. Experience the convenience of getting accurate answers to your questions from a dedicated community of professionals. Explore comprehensive solutions to your questions from knowledgeable professionals across various fields on our platform.

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