logo image

C++ program to print factorial of given number

Views: 13

Following program shows you how to print factorial of given number. In this program we get input from user and prints factorial of that number using for loop

#include <iostream>

int main() {
	int input;
	std::cout << "Enter a number:";
	std::cin >> input;
	int result = 1;
	for (int i = 1; i <= input; i++)
		result = result * i;
	std::cout << input << " factorial is: " << result;
}

Output:

Enter a number: 7
7 factorial is: 5040