Factorial Program in C++

The factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example, 6! = 1 x 2 x 3 x 4 x 5 x 6 = 720.
 

#include<iostream>

using namespace std;

int main(){
	unsigned long int product = 1;
	int initial = 1;
	int n;

	cout << "enter positive integer: ";
	cin >> n;

	if (n >= 0){
		while(initial <= n){
			product = product * initial;
			initial ++;
		}
	}
	else{
		cout << "error: please enter positive number";
		return 0;
	}

	cout << n << "! = " << product;
 	return 0;
}

 

output:

factorial code

I barely passed hope you do better

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.