Calculate factorial of large numbers !! (Using Singly Linked list)

#include <bits/stdc++.h>

using namespace std;

struct node
{
    int data;
    struct node *next;
};
struct node *add(struct node *tailint c)
{
    struct node *newnode;
    newnode = (struct node *)malloc(sizeof(struct node));
    newnode->data = c;
    newnode->next = 0;
    tail->next = newnode;
    tail = newnode;
    return tail;
}
//Using recursion to print the list in reverse order...
void print(struct node *head)
{
    if (head == 0)
        return;
    print(head->next);
    cout << head->data;
}
int main()
{
    struct node *head = 0, *temp, *newnode, *tail;
    int ncix;
    cout << "Enter the number: ";
    cin >> n;
    newnode = (struct node *)malloc(sizeof(struct node));
    newnode->next = 0;
    newnode->data = 1;
    //head=0 initially
    head = newnode;
    tail = newnode;

    for (i = 2i <= ni++)
    {
        c = 0;
        for (temp = headtemp != 0temp = temp->next)
        {
            x = (temp->data * i) + c;
            temp->data = x % 10;
            c = x / 10;
        }
        while (c != 0)
        {
            tail = add(tailc % 10);
            c /= 10;
        }
    }
    print(head);
}

Comments

Post a Comment

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)

Calculate factorial of large numbers !! (Using Arrays)

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