Find Factorial in C++

Hey, welcome back to new blog here you will learn how to fiind out the factorial of any number in C++.

We will find out a factorial very simple way so don't be confuse I'm here to help you.

So let's start coding of factorial...



Explanation =

Before you write the logic to find the factorial of any number first you need to define the header files in the program as you learn in C++ we use #include <iostream> so first, you have to write this header file.

After declaring the header files you need to write the using namespace std; if you are using Dev C++ then this is compulsory that you have to define. And if you are using Turbo C++ then you need not define this keyword.

After that, you have to start your main program using the keyword int main() and then open the brackets.

Now It's time to write your logic to find out the factorial of any number.

Logic = 

Before going to the logic first, we have to know what is meant by the factorial numbers.

"The factorial, symbolized by an exclamation mark (!), is a quantity defined for all integer s greater than or equal to 0. For an integer n greater than or equal to 1, the factorial is the product of all integers less than or equal to n but greater than or equal to 1. ... The factorial is of interest to number theorists."

To find the factorial of any number first you have to define the two variables first one is for the number that we will take form the user and then the second one will be to storing the factorial of the number initially the value of the factorial will take 1 and then print a message on the console window that "Enter a number "  then user will be entering a number after that apply the for loop in the for loop condition (int i=1;i<=num;i++) this the condition that we have to write in the condition of the for loop and inside the for loop we have to multiply the i value to the fact-value i value will be increment each time and then we display the value of the factorial.

This is the logic to find the factorial of any number.

Source code =

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int num;
int fact=1;
cout<<"\n Enter a number = ";
cin>>num;
for(int i=1;i<=num;i++)
{
fact = fact *i;
}
cout<<"\n Factorial of the number is = "<<fact;

}

Output =

Factorial of any number
So this is the way how you can find out the factorial of any number. If you have any doubt so contact me I will definitely help you.

Thank you

No comments:

Post a Comment