C++ PR OJE CT FIL E: Fact Orial OF ANY Numb ER

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 10

C++

PR
OJE
CT
FIL
E
FACT
ORIAL
OF
ANY
NUMB
ER

SAMARTH
DARGAN
0701112
USING LINKED LIST APPROACH
SOURCE CODE
#include<iostream>
#include<conio.h>
int num;
using namespace std;
typedef struct node
{
struct node(int data)
{
this->data = data;
previous = NULL;
next = NULL;
}
int data;
struct node* previous;
struct node* next;
} node;
class linkedlist
{
private:
node* front;
node* back;
public:
linkedlist();
~linkedlist();
void appendnode(int);
void displaynodes();

friend int enter(linkedlist *list);


void insertbeg(int);
friend int main();
};
linkedlist::linkedlist()
{
front = NULL;
back = NULL;
}
linkedlist::~linkedlist()
{
}
void linkedlist::insertbeg(int data)
{
node *n = new node(data);
n->previous = NULL;
n->next = front;
front->previous = n;
front = n;
}

void linkedlist::appendnode(int data)


{
node* n = new node(data);
if(back == NULL)
{
back = n;
front = n;
}
else
{
back->next = n;
n->previous = back;
back = n;
}
}
void linkedlist::displaynodes()
{
int i=0;
node* temp = front;
while(temp != NULL)
{
cout << temp->data;
temp = temp->next;
i+=1;
}
cout<<endl<<"total no of digits = "<<i;
}

int main()

{
linkedlist * list = new linkedlist();
num=enter(list);
node *curr;
curr = list->back;
int l ;
for(l=1;l<num;l++)
{
curr=list->back;
while(curr != NULL)
{curr->data=curr->data*l;
curr=curr->previous;
}
curr=list->back;
while(curr!=NULL)
{
if(curr->data>10)
{
if(curr->previous == NULL)
{
list->insertbeg(0);
}
curr->previous->data = curr->previous->data + curr-
>data/10;
curr->data = curr->data%10;
}
curr=curr->previous ;
}
}
list->displaynodes();
getch();
delete list;
return 0;
}
int enter(linkedlist *list)
{
int n,i,d,n2;
cout<<"enter the no. ";cin>>n;
n2=n;
i=1;
while(i<=n)
{
i*=10;
}
i/=10;
while(i>=1)
{
list->appendnode(n/i);
n=n-((n/i)*i);
i/=10;
}
return n2;
}
OUTPUT
enter the no. 1000
4023872600770937735437024339230039857193748642
1071463254379991042993851239862902059204420848
6969404800479988610197196058631666872994808558
9013238296699445909974245040870737599188236277
2718873251977950595099527612087497546249704360
1418278094646496291056393887437886487337119181
0458257836478499770124766328898359557354325131
8532395846307555740911426241747434934755342864
6576611667797396668820291207379143853719588249
8081268678383745597317461360853795345242215865
9320192809087829730843139284440328123155861103
6976801357304216168747609675871348312025478589
3207671691324484262361314125087802080002616831
5102734182797770478463586817016436502415369139
8281264810213092761244896359928705114964975419
9093422215668325720808213331861168115536158365
4698404670897560290095053761647584772842188967
9646244945160765353408198901385442487984959953
3191017233555566021394503997362807501378376153
0712776192684903435262520001588853514733161170
2103968175921510907788019393178114194545257223
8655414610628921879602238389714760885062768629
6714667469756291123408243920816015378088989396
4518263243671616762179168909779911903754031274
6222899880051954444142820121873617459926429565
8174662830295557029902432415318161721046583203
6786906117260158783520751516284225540265170483
3042261439742869330616908979684825901254583271
6822645806652676995865268227280707578139185817
8889652208164348344825993266043367660176999612
8318607883861502794659551311565520360939881806
1213855860030143569452722420634463179746059468
2573103790084024432438465657245014402821885252
4709351906209290231364932734975655139587205596
5422874977401141334696271542284586237738753823
0483865688976461927383814900140767310446640259
8994902222217659043399018860185665264850617997
0235619389701786004081188972991831102117122984
5901641921068884387121855646124960798722908519
2968193723886426148396573822911231250241866493
5314397013742853192664987533721894069428143411
8520158014123344828015051399694290153483077644
5690990731524332782882698646027898643211390835
0621709500259738986355427719674282224875758676
5752344220207573630569498825087968928162753848
8633969099598262809561214509948717012445164612
6037902930912088908694202851064018215439945715
6805941872748998094254742173582401063677404595
7417851608292301353580818400969963725242305608
5590370062427124341690900415369010593398383577
7939410970027753472000000000000000000000000000
0000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000
00000000000000000000000000000000000000
total no of digits = 2568

You might also like