Q:

C++ Program To Find Factorial

belongs to collection: Simple 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

57133839564458545904789328652610540031895535786011264182548375833179

82912484539839312657448867531114537710787874685420416266625019868450

446635594919592206657494259209573577892932535729044496247240541679072

2118445437122269675520000000000000000000000000000000000000

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 Read Integer (N) And Print First Th... >>
<< C++ Program To Find Character Is Vowel Or Not...