How to write C++ Program to find factorial of Number | Virtual Study Solutions

Adsetra Ads

 

Write C++ Program to find factorial of Number.

Today we will learn How to Find factorial of Number Using C++ Program.
But First of All lets discuss what is a Factorial. it is denoted by n!


Also Read: How to write C++ program to find Fibonacci Series

What is a Factorial ?

The factorial of a number 'n' is the product of all number from 1 up-to the number 'n'

it is denoted by n!. For example n=5 then factorial of 5 will be 1*2*3*4*5= 120. 5!= 120

write C++ Program to find factorial of Number
 Code of++ Program to find factorial of Number

Factorial program in C++ Logic:


  • First think what is the factorial of a number? 
  • How mathematically it can be calculated?
  • If you understand it now then it will be very easier to make a C++ Program logic to find the factorial.
  • User enters a number and we have to multiply all numbers up-to entered number.
  • Like if user enters 6 then Factorial should be equal to factorial= 1*2*3*4*5*6.
  • In this case a for Loop will be very helpful. It will start from one and multiply all numbers up-to entered number after it loop will be terminated.
  • Take a variable and initialized it to 1 and in loop store multiplication result into it like in below program a variable 
  • Factorial is used for this purpose.what is we does not initialized it to 1 and initialized it to zero or remain it uninitialized. In case of 0 our result will be zero in case of any number entered
  • In case of not initializing it our answer will correct mostly but if variable contains garbage value then we will not be able to get correct result. 
  • It is recommended that to initialize it to one.
Also Read: How to Write C++ Program to Find Prime Number

C++ Program to find factorial of Number


#include<iostream>

using namespace std;

int main()

{

int num,factorial=1;

cout<<" Enter Number To Find Its Factorial: ";

cin>>num;

for(int a=1;a<=num;a++)

{

factorial=factorial*a;

}

cout<<"Factorial of Given Number is ="<<factorial<<endl;

return 0;

}

Also Read: How to write C++ Program to find factorial of Number


If you have any queries related to How to write C++ program to find factorial of Number please comment below:

Post a Comment

  1. ma saray lecture sunta ho per kuch kuch smj ma ata hai or example b book say dk kr krta ho per smj nahi ati kuch ...!
    Mjy b sikho easy way say

    ReplyDelete

 

Top