Hay guys, i wanted to get the code for a very large factorial but i was unable to do so. Now, i had to work and develop the code myself, which i did.
This code calculates factorial of numbers uptil 14000. . i havent tried more. But i guarantee this code's working. I hope you will find this code helpful.
Atleast u'll not have to waste time like i did!!!!
Code for finding factorial of a very large number
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GROUP 12
// Group members:Aniqa Hayee(091105), Nimra Mustafa(091133), Javeria Raja(091120)
//
// BS(CS)-Semester I-Section B
// AIR UNIVERSITY<ISLAMABAD,PAKISTAN>
//
//Computer Programming
//Project:To calculate the factorial of a large number
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include<iostream.h>
void main()
{
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
long k;
cout<<"Enter a number whose factorial needs to be calculated:";
cin>>k;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////code for numbers which are less than 33 and greater than 3///////////////////////////////////////////////////////////
if (k<=33)
{
unsigned double long fact=1;
fact=1;
for(int b=k;b>=1;b--)
{
fact=fact*b;
}
cout<<"The factorial of "<<k<<" is "<<fact<<endl;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////code for numbers which are greater than 33 ///////////////////////////////////////////////////////////
else
{
int numArr[10000];
int total,rem=0,count; //rem use to save remainder of division(Carry Number).
register int i; //register will enhance performance by minimizing access time
//int i;
for(i=0;i<10000;i++)
numArr[i]=0; //set all array on NULL.
numArr[10000]=1; //start from end of array.
for(count=2;count<=k;count++)
{
while(i>0)
{
total=numArr[i]*count+rem; //rem will add the carry number to the next array digit that is multiplied to the count
rem=0;
if(total>9)
{
numArr[i]=total%10;
rem=total/10;
}
else
{
numArr[i]=total; //numArr[i] is being accessed in this for loop because we have made i as register which means the memory is allocated
}
i--;
}
rem=0;
total=0;
i=10000;
}
cout<<"The factorial of "<<k<<" is ";
for(i=0;i<10000;i++)
{
if(numArr[i]!=0 || count==1)
{
cout<<numArr[i];
count=1;
}
}
cout<<endl;
}
}
Aniqa Shaikh 0 Newbie Poster
Nick Evan 4,005 Industrious Poster Team Colleague Featured Poster
Narue 5,707 Bad Cop Team Colleague
Salem commented: Go get 'em! +18
xavier666 commented: Narue is a surgeon of code +1
Aniqa Shaikh 0 Newbie Poster
Agni 370 Practically a Master Poster Featured Poster
Salem 5,199 Posting Sage
Narue 5,707 Bad Cop Team Colleague
mr_deb 0 Newbie Poster
sahasrara 0 Newbie Poster
sameershah21 0 Newbie Poster
sameershah21 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.