August 12, 2024

# C++ Program to Calculate Factorial of a number

Question

Write a C++ program that calculates the factorial of a number using a function. The Program should use a While Loop. The program prompts the user to enter an integer from the standard input, calculates and displays the factorial of the inputed number. Draw the Flowchart

The factorial of a number is obtained by continuously multiplying the number with lower integers in steps of 1.

Fatorial of n is given by

Factorial n = n * (n-1) * (n-2) *… * 2 * 1

Factorial 5 = 5 *4 * 3 * 2 * 1 = 120

Flowchart is given below

For the decision, the control continues downwards for NO and branches to the right for YES

C++ Program is given below

//Functions computes the factorial of a number
//By Kindson Munonye
//Date: February 2013
//Date Modified: February 2013
//Location: Port Harcourt, Nigeria
#include ;
#include ;

using namespace std;

int factorial(int n)
{
int fact = 1;
while(n&lt;1) {
fact = fact * n;
n--;
}
return fact;
}

int main()
{
int num;
cout&lt;&lt;"Enter an integer: "; cin&gt;&gt;num;
cout&lt;&lt;"\n\n Factorial "&lt;&lt;num&lt;&lt;" = "&lt;&lt;factorial(num)&lt;&lt;endl;
system("pause");
return 0;
}

#### kttpro

View all posts by kttpro →

## 4 thoughts on “C++ Program to Calculate Factorial of a number”

1. Iragoldinvestments.Org says:

looking for a related matter, your web site came up, it seems to be good.
I’ve bookmarked it in my google bookmarks.
Google, and found that it’s really informative. I’m going to watch out for
brussels. I’ll appreciate for those who continue this in future.
Many other folks might be benefited from your writing. Cheers!

2. Top 3 ira Gold Companies says:

Hey very nice blog!! Guy .. Beautiful .. Wonderful ..
I’ll bookmark your site and take the feeds also? I’m glad to search out
numerous helpful information here in the submit, we
want develop more techniques on this regard, thanks for
sharing. . . . . .

3. gold ira companies says:

These are really wonderful ideas in regarding blogging.
You have touched some pleasant things here. Any way keep up wrinting.