Algorithm
and c program to calculate factorial of 100 or large number in c
#include<stdio.h>
#define MAX 10000
void factorialof(int);
void multiply(int);
int length = 0;
int fact[MAX];
int main(){
int num;
int i;
printf("Enter
any integer number : ");
scanf("%d",&num);
fact[0]=1;
factorialof(num);
printf("Factorial
is : ");
for(i=length;i>=0;i--){
printf("%d",fact[i]);
}
return 0;
}
void factorialof(int num){
int i;
for(i=2;i<=num;i++){
multiply(i);
}
}
void multiply(int num){
long i,r=0;
int arr[MAX];
for(i=0;i<=length;i++){
arr[i]=fact[i];
}
for(i=0;i<=length;i++){
fact[i]
= (arr[i]*num + r)%10;
r
= (arr[i]*num + r)/10;
//printf("%d
",r);
}
if(r!=0){
while(r!=0){
fact[i]=r%10;
r=
r/10;
i++;
}
}
length
= i-1;
}
Note: You can
change the value of MAX to find factorial of too large numbers.
Factorial of 100 from above code is:
933262154439441526816992388562667004907159
682643816214685929638952175999932299156089
414639761565182862536979208272237582511852
1091686400000000000
682643816214685929638952175999932299156089
414639761565182862536979208272237582511852
1091686400000000000
Logic for factorial of large numbers
As we know in c there are not any
such data types which can store a very large numbers. For example we want to solve the expression:
100!
Factorial of 100 is very big number
which beyond the range of even long int or long double. Then question is how to store such a big
numbers in c?
Solution is very simple i.e. using
array. Above program has used same logic that is we are using as usual logic to find
out the factorial of any number except instead of storing the data in the
normal variables we are storing into the array.
0 comments:
Post a Comment