Q:

C++ Program To Find Factorial

belongs to collection: Loop Programs In C ++Programming

0

Write A C++ Program To Calculate Factorial Of A Given Number or factorial program in c or Factorial For Large Number Using Array.

What Is Factorial:-

 In mathematics, the factorial of a non-negative integer n, denoted by n! is the product of all positive integers less than or equal to n. For example,

5! = 5 * 4 * 3 * 2 * 1 = 120.

The value of 0! is 1, according to the convention for an empty product *

The factorial operation is encountered in many areas of mathematics, notably in combinatorics, algebra, and mathematical analysis. Its most basic occurrence is the fact that there are n!. This fact was known at least as early as the 12th century, to Indian scholars.
Source:- Wikipedia


Logic:-

 Suppose we want to find factorial of number 6 then we can multiply number from 1 to 6 (n) like 1 * 2 * 3 * 4 * 5 * 6 = 720. So we can use a loop and run the loop from 1 to number n (the number whose factorial you want) and multiply a number, this is a simple way to find a factorial. You can also find the factorial program for Large Number.


Factorial Use's in Real Life:-
1. Trigonometry,
2. The exponential function
3. Permutations and Combinations
4. Calculus (Like Taylor’s Series and Maclaurin Series) 
5. Data Distributions

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Factorial For Large Number Using Array

#include <cstring>
#include <iostream>
#include <cstdlib>
#define ll long long
using namespace std;
int fact[10001][10000] = {0};
void fact_large(int n)
{
    int i;
    fact[1][0] = 1;
    fact[1][1] = 1;
    if (fact[n][0] == 0) 
    {
        for (i = n - 1; i > 0 ; i--) 
        {
            if (fact[i][0] != 0)
                break;
        }
        for ( ; i < n; i++) 
        {
            int j = 1;
            int carry = 0;
            int len = fact[i][0];
            while (len--)
            {
                int temp = (i + 1) * fact[i][j] + carry;
                fact[i + 1][j] = temp % 10;
                carry = temp / 10;
                j++;
            }
            while (carry > 0) 
            {
                fact[i + 1][j] = carry % 10;
                carry /= 10;
                j++;
            }
            fact[i + 1][0] = j - 1;
        }
    }
    for (i = fact[n][0]; i > 0; i--) 
    {
        cout << fact[n][i];
    }
    cout<<endl;
}

int main()
{
    int n,t;
     cout<<"Enter The Number \n";
        cin>>n;
        fact_large(n);
    return 0;
}

 

Output:

Enter The Number 

150

5713383956445854590478932865261054003189553578601126418254837583317

9829124845398393126574488675311145377107878746854204162666250198684

5044663559491959220665749425920957357789293253572904449624724054167

90722118445437122269675520000000000000000000000000000000000000

need an explanation for this answer? contact us directly to get an explanation for this answer
need an explanation for this answer? contact us directly to get an explanation for this answer

 Factorial Using For Loop

#include<iostream>
using namespace std;

int main()
{
  
 unsigned long long int fact=1;
  int i,num;
  
 cout<<"Enter The Number. You Want Factorial:";
  cin>>num;
  
 for(i=1;i<=num;i++)
    {
        fact=fact*i;
    }
    
 cout<<"\nFactorial of "<<num<<" Is = "<<fact<<endl;
    return 0;

}

 

Output:

Enter The Number. You Want Factorial:6

Factorial of 6 Is = 720

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (2)

C++ Program To Print A Message Multiple Times... >>