jakaria_hossain

Big number factorial

Oct 23rd, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. #include<stdio.h>
  2. #define MAX 5000
  3. void factorial(int n)
  4. {
  5. int result[MAX];
  6. result[0]=1;
  7. int i,res_size=1;
  8. for(i=2;i<=n;i++)res_size=multiply(i,result,res_size);
  9. printf("FACTORIAL OF GIVEN NUMBER IS\n");
  10. for(i=res_size-1;i>=0;i--)printf("%d",result[i]);
  11. }
  12.  
  13. int multiply(int x,int res[],int res_size)
  14. {
  15. int i,carry=0;
  16. for(i=0;i<res_size;i++)
  17. {
  18. int prod=res[i]*x+carry;
  19. res[i]=prod%10;
  20. carry=prod/10;
  21. }
  22. while(carry)
  23. {
  24. res[res_size]=carry%10;
  25. carry=carry/10;
  26. res_size++;
  27. }
  28. return res_size;
  29. }
  30.  
  31. int main()
  32. {
  33. int n;
  34. scanf("%d",&n);
  35. factorial(n);
  36. return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment