• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C++计算一个超级大数的阶乘算法

OC/C/C++ 水墨上仙 1230次浏览

C++计算一个超级大数的阶乘算法

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


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C++计算一个超级大数的阶乘算法
喜欢 (0)
加载中……