-->





Translate

Program to find factorial of Number in C++

64 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

64 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. *******
    ***
    **
    *

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

    ReplyDelete
  4. 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
  5. *******
    *****
    ***
    *
    #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
  6. 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
  7. what is seekg(), seekp(), tellg(), tellp()???

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

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

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

    ReplyDelete
  11. #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
  12. a linked queue program hw i can make function that deletes all my nodes

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

    ReplyDelete
  14. #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
  15. Fahad use Code Mirror to show your code .. It will give your Code a better look and people can easily copy your code..

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

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

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

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

    ReplyDelete
  20. 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
  21. 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
  22. give me code for find fictorials of numbers 1 to 5 using for loop

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

    ReplyDelete
  24. 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
  25. why we should take factorial=1 when declaring the variable ?

    ReplyDelete
  26. 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
  27. Is it possible to do this program using while or do while??

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

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

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

    ReplyDelete
  31. HOW TO WRITE SAME PROGRAM WITHOUT USING NAMESPACE STD ?????

    ReplyDelete
  32. How to calculate sum of new natural numbers using "function" in C++ ?

    ReplyDelete
  33. Hey can u provide me aa query
    Q.Write a program that get 5 values in array from user and caculate the factorial of each value and store them in an other array

    ReplyDelete
  34. Plz provide me the following code
    Write a program that get 5 values from user in an array and calculte the factorial of each value and store them inan other array

    ReplyDelete
  35. Take a no froma a user and print its table.

    ReplyDelete
  36. Take a no from a user and print its fractional value.

    ReplyDelete
  37. can you do it using user-defined functions

    ReplyDelete
  38. how to get factorial without using multipliation

    ReplyDelete