Ask your own question, for FREE!
Computer Science 17 Online
OpenStudy (anonymous):

How to calculate factorial of any large no. like 56? plz give me logic..... i prefer C for coding this

OpenStudy (zeeporo):

You will need a loop that goes till the last number (e.g. 56), you will need a variable that stores the actual result (result sounds good) and in the loop you only have to multiply with the actual number. Variable result should be initiated with 1. I write a snippet just a sec.

OpenStudy (zeeporo):

I am not too good in C nowadays... but something like this would do. result = 1; // I would use long not int if you want big numbers... for(count=2;count<=n;count++) //terminates if count>n (n is 56 e.g.) { result = result * count; }

OpenStudy (anonymous):

@zeeporo : i know the logic but the problem occurs when it we give the number like 56 as an input islarger i m stuck with the datatype thing.. whichdatatype it should take..

OpenStudy (anonymous):

I don't think the computer has enough memory to calculate that number as the factorial of 56 is: 710998587804863451854045647463724949736497978881168458687447040000000000000

OpenStudy (anonymous):

A program to calculate factorial would look something like this though: #include<stdio.h> int main(){ int i,f=1,num; printf("Enter a number: "); scanf("%d",&num); for(i=1;i<=num;i++) f=f*i; printf("Factorial of %d is: %d",num,f); return 0; }

OpenStudy (e.mccormick):

You may need a library for large numbers, or you may need to write something that stores the number in another way, such as a string, and then have it do the work to go back and forth in sections. This makes any mathematical operation fall within the limits of c data types. A real world example is the abacus. If you make a computerized version of an abacus, each part of an array would represent one digit in the number. Then the math is done progressively through the digits. In the end, you simply print them out in order to produce very large values.

OpenStudy (anonymous):

So as @e.mccormick said, you could try storing the digits in an array. #include<stdio.h> int main() { int t; int a[200]; //array will have the capacity to store 200 digits. int n,i,j,temp,m,x; while(t--) { printf("Enter a number: "); scanf("%d",&n); a[0]=1; //initializes array with only 1 digit, the digit 1. m=1; // initializes digit counter temp = 0; //Initializes carry variable to 0. for(i=1;i<=n;i++) { for(j=0;j<m;j++) { x = a[j]*i+temp; //x contains the digit by digit product a[j]=x%10; //Contains the digit to store in position j temp = x/10; //Contains the carry value that will be stored on later indexes } while(temp>0) //while loop that will store the carry value on array. { a[m]=temp%10; temp = temp/10; m++; // increments digit counter } } for(i=m-1;i>=0;i--) //printing answer printf("%d",a[i]); printf("\n"); } return 0; }

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!