Calculate factorial of large numbers !! (Using Arrays)

#include <bits/stdc++.h>
using namespace std;
#define max 10000
int main()
{
    int ncjkxi;
    int a[max];
    cout << "Enter the number: ";
    cin >> n;
    j = 0; // j will be pointing to new index created to stored carry
    a[0] = 1;
    for (i = 2i <= ni++)
    {
        c = 0; //carry when each number of array is multiplied by i 
        //(i is a part of numbers that come in factorial)
        //every number will be multiplied to array from starting index.
        for (k = 0k <= jk++)
        {
            x = (a[k] * i) + c;
            a[k] = x % 10;
            c = x / 10;
        }
        //In case end carry is of more than one digit...
        //digits are stored in reverse order..
        while(c!=0)
        {
            a[++j]=c%10;
            c/=10;
        }
    }
    //print array in reverse order..
    for (i = ji >= 0i--)
        cout << a[i];
}

Comments

Popular posts from this blog

First_Come_First_Serve CPU Scheduling

Reversing stack Method 2 !! (One Helper Stack only)

Populating Next Right Pointers in Each Node in O(1) space (without queue and level order)

Multiplication of large numbers (Given in string format)

Left View of Binary Tree (Method 1 using recursion)

Check Bracket Sequence

Image Multiplication

Boundary Traversal of binary tree

BST to greater sum tree