Code for finding factorial of a very large number

Updated Aniqa Shaikh 1 Tallied Votes 662 Views Share

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!!!!

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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

and yes. . . .this is the easiest code there is. . . u'll understand it just as u keep reading it!!

Nick Evan 4,005 Industrious Poster Team Colleague Featured Poster

This code won't work. The very first line is already wrong: #include<iostream.h> . Any compiler from the last decade will tell you "include file not found".
At that's just the first of many problems with standard-C++ in your code.

Narue 5,707 Bad Cop Team Colleague

Atleast u'll not have to waste time like i did!!!!

How can it be a waste of time when you learn how to do something? You're basically just preemptively doing everyone's homework for them with this code snippet (seeing as how this program was homework, judging by the file comment).

Anyway, the code is filled with assumptions and poor practices. Here are a few of the ones that stick out:

>#include<iostream.h>
Not standard, becoming more and more scarce as modern compilers start refusing to compile it.

>void main()
main returns int. It always has, even back to the prehistory of C (C++'s primary parent language).

>if (k<=33)
You're making assumptions about the range of long double.

>//register will enhance performance by minimizing access time
In practice, register will probably be ignored by your compiler. Even if this hint is honored, the effect is likely to be detrimental as the compiler is far better at allocating registers than an application programmer. As such, adding restrictions when there's no guaranteed (or even likely) benefit is silly.

>int numArr[10000];
The array size is an unwarranted assumption.

>numArr[10000]=1; //start from end of array.
You mean start by overflowing the array. In an array of 10000 elements, the last accessible index is 9999.

>//numArr is being accessed in this for loop because we have
>made i as register which means the memory is allocated

Um, i is an automatic variable anyway (as is numArr). If you got to this point, it's allocated, regardless of being declared as register or not. Your comment is nonsensical and unnecessary.

Salem commented: Go get 'em! +18
xavier666 commented: Narue is a surgeon of code +1
Aniqa Shaikh 0 Newbie Poster

Well, i have used this program in visual C++ 6.0. And it works perfectly. And i have n't posted this code without consulting a proper teacher. I am a beginner, and so this code has been checked by a proper teacher. I don't know what compiler you are using, but this code works positively and perfectly for me!
if this code helps people, i'll be very happy.

Agni 370 Practically a Master Poster Featured Poster

If you are really interested in learning C++ and becoming a good programmer, take all this advice constructively. Tomorrow when you go out to work in the professional world and do these mistakes, no one will point them out to you, they would probably just fire you. So, there's no point becoming all obstinate about your code, you are in-fact lucky that you got your code reviewed by such good programmers and got all this feedback.
Be wise and try to understand every bit of what's given and importantly, don't take your teacher's word as final.

xavier666 commented: nice one! +1
tux4life commented: Very nice suggestion :) +6
Salem 5,199 Posting Sage

> Well, i have used this program in visual C++ 6.0.
VC6 is over 10 years old. The world has moved on, and so have the standards for C++.

> And i have n't posted this code without consulting a proper teacher.
Ever hear the adage "those that can, do; those that can't, teach"?
It is our considerable experience which suggests there are a lot of teachers out there who couldn't find their own ass in the dark, nevermind teach C++ (or programming in general) with any degree of competence.

Just because someone who claims to be your teacher has checked the code does NOT make it bug free (Narue trivially proved this to be the case with your array overstep), but your teacher didn't spot that did they?

In the real world (if you hope to program beyond college), your code will be reviewed many many times by lots of people with vastly more experience of programming than either you or your teacher. Mistakes will always be found.

Narue 5,707 Bad Cop Team Colleague

>Well, i have used this program in visual C++ 6.0. And it works perfectly.
It crashes and burns in Visual C++ 2005 and 2008, but that's after I fix the code to be more standard. Before that it doesn't even compile. I guess you're behind the times. Not a good thing when you're likely to use the more modern versions out in the real world.

>I am a beginner, and so this code has been checked by a proper teacher.
Yes, you are a beginner. And this code may have been checked by a teacher, but certainly not a proper teacher. A proper teacher would have seen the glaring overflow bug.

>Mistakes will always be found.
Yes, and it's preferred that mistakes be found before the code goes live. Because fixing a mistake in production is far more expensive and time consuming than fixing a mistake during development.

mr_deb 0 Newbie Poster

Guys please check the Logic ...Dont go by syntax ,any one can fix this compilation error later on,you should focus on the logic what he is written .
And dont forget in professional life no one starts with blasting people learn from failure.Any way gCC when you compile that code it will give the warning whatever you guys have pointed out.So its not a big deal to fix them

sahasrara 0 Newbie Poster

unsigned long double is false declaration .
float and double date type must be signed number.
sign bit is the first bit of them memory style.

sameershah21 0 Newbie Poster

I dont know why everyone complains about small things when someone achieves something astronomical. If this is code is indeed written by the named people, the have done terrific job! Thank you, I will follow the steps you have written and understand it to see if I can implement in other problems as well.
Thank you.

sameershah21 0 Newbie Poster

This problem is asked in project euler problem 20

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.