w3resource

C++ Exercises: Find the factorial of a number

C++ For Loop: Exercise-7 with Solution

Write a program in C++ to find the factorial of a number.

Pictorial Presentation:

C++ Exercises: Find the factorial of a number

Sample Solution :-

C++ Code :

#include <iostream>
using namespace std;
int main()
{
    int num1,factorial=1;
    cout << "\n\n Find the factorial of a number:\n";
	cout << "------------------------------------\n";
	cout << " Input a number to find the factorial: ";
	cin>> num1;
    for(int a=1;a<=num1;a++)
    {
        factorial=factorial*a;
    }
	cout<<" The factorial of the given number is: "<<factorial<<endl;
    return 0;
}

Sample Output:

 Find the factorial of a number:                                       
------------------------------------                                   
 Input a number to find the factorial: 5                               
 The factorial of the given number is: 120 

Flowchart:

Flowchart: Find the factorial of a number

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C++ to find prime number within a range.
Next: Write a program in C++ to find the last prime number occur before the entered number.

What is the difficulty level of this exercise?



Share this Tutorial / Exercise on : Facebook and Twitter