ccplusplus.com
Learn C, C++ Concepts
Saturday, September 10, 2011
factorial of large numbers in c
/****************************************************** * File : factorial-of-large-numbers.c * Author : Saurabh Gupta * Desc : factorial calculator large numbers * Source : http://saurabhgupta0527.blogspot.com/ * Created : PM 05:51 10 September 2011 *****************************************************/ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #define MAX_NUMBER_VAL 10000 void computeFactorial(int); void multiply(int); int nLength = 0; int fact[MAX_NUMBER_VAL]; int main(){ int nFactorialNumber; int i; printf("Enter any integer number : "); scanf("%d",&nFactorialNumber); fact[0]=1; computeFactorial(nFactorialNumber); printf("Factorial is : "); for(i = nLength; i >= 0; i--){ printf("%d",fact[i]); } printf ("\n"); return 0; } void computeFactorial(int nFactorialNumber) { int i; for(i=2;i<=nFactorialNumber;i++){ multiply(i); } } void multiply(int nFactorialNumber) { long i,r = 0; int arr[MAX_NUMBER_VAL]; for(i = 0;i <= nLength; i++) { arr[i]=fact[i]; } for(i = 0; i <= nLength; i++) { fact[i] = (arr[i]*nFactorialNumber + r)%10; r = (arr[i]*nFactorialNumber + r)/10; } if(r!=0){ while(r != 0){ fact[i]=r%10; r = r/10; i++; } } nLength = i-1; } /* * OUTPUT * [sgupta@rhel54x64 c]$ gcc factorial-of-large-numbers.c -o factorial-of-large-numbers [sgupta@rhel54x64 c]$ ./factorial-of-large-numbers Enter any integer number : 10 Factorial is : 3628800 [sgupta@rhel54x64 c]$ */
See also
Other popular tricky C Sample Codes and language Concept
.
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment