-->




Program to find factorial of Number in C++

55 comments
What is a Factorial of a number 'n'?
 The factorial of a number 'n' is the product of all number from 1 upto 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
Factorial program C++ Logic:
  • First think what is the factorial of a number? How mathematically it can be calculated.
  •  If you got this info 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 upto 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 upto 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.

C++ code to find prime number using for loop

#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;

}

Image View Of Program 



If you have any query comment below. Thanks

55 comments:

  1. Easy & nice programme .
    your program are going to help me a lot .
    thank you !

    ReplyDelete
    Replies
    1. if i input 34
      the factorial is 0

      Delete
    2. Then take long int inplace of only int

      Delete
    3. Factorial of 34 is returning 0 because you should use long long int data type for factorial.
      Previous example produces the correct answer until 12! = 479001600.
      Using long long int will work until 20.

      Delete
  2. Nice program.
    Good website, I had been searching for a good C++ website, now I think I got it

    ReplyDelete
  3. Very nice work... You are helping lots of students by sharing your knowledge with them!!

    ReplyDelete
    Replies
    1. Thanks Very Much Arundhati malik thakur. Keep Visiting.
      Have A Look To Latest Programs

      Find Palindrome Number in C++

      Delete
  4. *******
    *****
    ***
    *
    #include
    #include
    void main()
    {
    int a,b;
    for(a=5;a>=1;a--)
    {
    for(b=1;b<=a;b++)
    {
    printf("*");
    }
    printf("\n");
    }
    getch();
    }

    ReplyDelete
    Replies
    1. can you provide me a program
      Q: create a program on finding the roots of quadratic equation using for loop?

      Delete
  5. hello any one,,could u please help me with my situation..i need to make a coding base on formulae
    ∑_(n=0)^m▒〖1/n!=1/0!+1/1!+1/2!+⋯….+1/n!〗....the question is,,ask user to enter the integer m,,then use the formulae,,i dont know

    ReplyDelete
  6. what is seekg(), seekp(), tellg(), tellp()???

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Can you help me to find a factorial of a number without using loop and if else

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. #include "stdafx.h"
    #include
    using namespace std;
    int factorial(int n)
    {

    cout<<"I am calculating: F("<>n;
    int result = factorial(n);
    cout<<result<<endl;
    return 0;
    }

    ReplyDelete
    Replies
    1. can u give me a program on 0!=1 using for loop

      Delete
  11. a linked queue program hw i can make function that deletes all my nodes

    ReplyDelete
  12. a linked queue program hw i can make function that deletes all my nodes

    ReplyDelete
  13. #include

    int main()
    {
    int i, n, factorial=1;
    printf("Enter Number to Find Factorial: ");
    scanf("%d", &n);

    for(i=1; i<=n; i++)
    {
    factorial=factorial*n--;
    }

    printf("Factorial: %d", factorial);

    return 0;
    }

    ReplyDelete
  14. Fahad use Code Mirror to show your code .. It will give your Code a better look and people can easily copy your code..

    ReplyDelete
  15. can you give me a code using a loop please

    ReplyDelete
  16. Nice programs. Pls, I need a program to calculate zeno' s paradox.

    ReplyDelete
  17. plz find the factorial of 5 in do while loop

    ReplyDelete
  18. thanks now it is easy to understand how factorial find

    ReplyDelete
  19. How to write a program for the sum of this series

    Sum= 1/1! + 2/2! + 3/3! + 4/4! + 5/5! + 6/6!

    ReplyDelete
    Replies
    1. Write a complete code to add first seven terms of the following series using nested loops.
      1/1! + 2/2! + 3/3! + …. 7/7!
      If u have any idea about this please help me

      Delete
  20. Write a complete code to add first seven terms of the following series using nested loops.
    1/1! + 2/2! + 3/3! + …. 7/7!

    Please help me fast

    ReplyDelete
  21. give me code for find fictorials of numbers 1 to 5 using for loop

    ReplyDelete
  22. Please help me out I can't find the answer of:
    2/2! - 3/3 + 4/4!
    Please write complete code for this..

    ReplyDelete
  23. 1-(x^3/3!)+(x^5/5!)-(x^7/7!)...
    wap to input he values of x and n sum up n terms of the above series

    ReplyDelete
  24. why we should take factorial=1 when declaring the variable ?

    ReplyDelete
  25. i did it this way because , our teacher told to include zero factorial in the program. please correct the program.


    #include

    using namespace std;

    int main()
    {
    int n,i=0,factorial ;
    cout<<"Enter a number"<<endl;
    cin << n ;
    cout << " factorial = n! "<<endl;

    for(i=0; i<=n;i++ )
    {
    if (i=0)
    factorial n = 1;

    else
    factorial = factorial* i;

    }
    cout<<"factorial of the number is = " <<factorial<<endl;
    return 0;
    }

    ReplyDelete
  26. Is it possible to do this program using while or do while??

    ReplyDelete
  27. This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.

    Love from

    ReplyDelete
  28. Programming is combination of intelligent and creative work. Programmers can do anything with code. The entire Programming tutorials that you mention here on this blog are awesome. Beginners Heap also provides latest tutorials of Programming from beginning to advance level.
    Be with us to learn programming in new and creative way.

    ReplyDelete
  29. typing using namespace std;
    is giving me syntax error can any help me.

    ReplyDelete
  30. how to display maximum and minimum number in an array of 10 numbers

    ReplyDelete
  31. Which function is used in c++ to clear screen.Like clrscr () in c

    ReplyDelete