# Answer to Question #13817 in C++ for Darryl kaye Sanga

Question #13817

Write a program that will compute for n! (n factorial) which is the product of all numbers from 1 to n.

Expert's answer

#include <iostream.h>

int factorial(int);

void main(void) {

int number;

cout << "Please enter a positive integer: ";

cin >> number;

if (number < 0)

cout << "That is not a positive integer.

";

else

cout << number << " factorial is: " << factorial(number) << endl;

}

int factorial(int number) {

int temp;

if(number <= 1) return 1;

temp = number * factorial(number - 1);

return temp;

}

int factorial(int);

void main(void) {

int number;

cout << "Please enter a positive integer: ";

cin >> number;

if (number < 0)

cout << "That is not a positive integer.

";

else

cout << number << " factorial is: " << factorial(number) << endl;

}

int factorial(int number) {

int temp;

if(number <= 1) return 1;

temp = number * factorial(number - 1);

return temp;

}

## Comments

## Leave a comment