Idea : - we are going to find factorial of a large number number.
Factorial of large number |
- store num in array such that each element of array is a single digit.
- Decrease num by 1 and multiply num with each element of array and then for each element we store them as the rightmost digit remains at it's location while the remaining part is added to the next element and the last element is again stored as single digit and the remaining is stored on next till remaining part becomes 0.
- //C Program to find factorial of very large number
- #include<stdio.h>
- #include<stdlib.h>
- void factorial(int num)
- {
- int temp,i,k=0,a[200]={0};
- a[0]=num--;
- while(num){
- for(i=0;i<=k;i++)
- /* multiply each element of array with num */
- a[i]*=num;
- /* make each element of array as a single digit */
- for(i=0;i<=k;i++)
- {
- temp=a[i]/10;
- a[i]=a[i]%10;
- a[i+1]=temp+a[i+1];
- /* if i=k then we have reached the last element of array so */
- if(i==k)
- {
- temp=a[k+1];
- /* we again split the a[k] into single digit and */
- while(temp>0)
- {
- k++;
- /* remaining part is stored in next location */
- a[k]=temp%10;
- temp=temp/10;
- }
- /* now set i=k as number of element in array is i now */
- i=k;
- }
- }
- num--;
- }
- for(i=k;i>=0;i--)
- printf("%d",a[i]);
- }
- main()
- {
- int num;
- printf("Enter the number : ");
- scanf("%d",&num);
- factorial(num);
- printf("\n");
- system("pause");
- }
Thanks bro for this
ReplyDeletejo bhi hai accha h :P
DeleteVery helpful tutorial.I have interest in programming language but most of the time i did not get the tutorials or example.But this blog giving the correct information.I am not an expert in wring so i took the guidelines from the custom essay service.
ReplyDelete