Guest User

Untitled

a guest
Oct 21st, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.83 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAX 375         //factorial of 200 has 375 digits
  4.  
  5. /*
  6.     Name        : Rasesh Mori
  7.     Roll        : 201105518
  8.     Assignment  : 0
  9.     Input       : Number for which factorial is to be found (0<=n<=200)
  10.     Ouput       : Factorial of the input number
  11.     Description : Case 1: input<9(i.e. factorial value can be stored in integer variable)
  12.                   * Find factorial by multiplying all the numbers till the number
  13.  
  14.               Case 2: input>=9
  15.                   * Store the result in an array such that each array variable stores one digit of output and simulate multiplication
  16. */
  17.  
  18.  
  19. int main()
  20. {
  21.     int factorialNum=1; //number of which factorial is to be found
  22.     int counter;       
  23.     int factorialValue; //stores result if can be accomodated in integer range
  24.     int factArray[MAX]; //stores factorial of large numbers
  25.     int index;     
  26.     int carry = 0;      //used to store carry for multiplication in array
  27.     int arrayValue;     //temporary storage of calculation
  28.     int prefixZeroFlag = 1; //used while printing final answer to remove initial zeroes
  29.  
  30.    
  31.     //Get input
  32.     printf("\n Enter number for which factorial is to be found: ");
  33.     if(scanf("%d",&factorialNum) == 0)
  34.     {
  35.         printf("\n\nInvalid input: Please enter an integer in range [0-200]\n\n");
  36.         return 1;
  37.     }
  38.  
  39.  
  40.     //Check for correctness of input
  41.     if(factorialNum < 0)   
  42.     {
  43.         printf("\n\nFactorial for negative number is undefined.\n\n");
  44.         return 1;
  45.     }
  46.     else if(factorialNum > 200)
  47.     {
  48.         printf("\n\nSorry. This program is designed to find factorial for numbers <= 200.\n\n");
  49.         return 1;
  50.     }
  51.  
  52.  
  53.  
  54.     //If result can be accomodated in integer, use traditional method.
  55.     if(factorialNum < 9)   
  56.     {
  57.         factorialValue = 1;
  58.  
  59.         for(counter = 1; counter <= factorialNum; counter++)
  60.         {
  61.             factorialValue *= counter;
  62.         }
  63.         printf("\nFactorial of %d is: %d",factorialNum, factorialValue);   
  64.     }
  65.     else    //Perform multiplication storing each digit in array
  66.     {
  67.         index=MAX-1;
  68.         factArray[index] = 1;
  69.  
  70.         //initialize array to 0
  71.         for(counter=0; counter < MAX-1; counter++)
  72.         {
  73.             factArray[counter]=0;
  74.         }
  75.        
  76.  
  77.         for(counter=2; counter<=factorialNum; counter++)
  78.         {
  79.             while(index>=0)
  80.             {
  81.                 arrayValue = factArray[index] * counter + carry;    //Perform multiplication adding previous carry, of any
  82.                
  83.                 carry=0;
  84.                 if(arrayValue <= 9)
  85.                 {
  86.                     factArray[index] = arrayValue;
  87.                 }                           //If result >9, calculate carry and use in next mul.
  88.                 else
  89.                 {
  90.                     factArray[index] = arrayValue % 10;
  91.                     carry = arrayValue / 10;
  92.                 }
  93.                 index--;
  94.             }
  95.  
  96.             carry=0;
  97.             arrayValue=0;
  98.             index=MAX-1;
  99.         }
  100.  
  101.  
  102.         printf("\nFactorial of %d is: ",factorialNum);
  103.  
  104.         for(index=0;index<MAX;index++)                      //Printing result
  105.           {
  106.             if(factArray[index]==0 && prefixZeroFlag)
  107.                 continue;
  108.             else
  109.             {
  110.                 prefixZeroFlag=0;
  111.                 printf("%d",factArray[index]);
  112.             }
  113.                
  114.         }
  115.     }
  116.    
  117.     printf("\n\n");
  118.     return 0;
  119. }
Add Comment
Please, Sign In to add comment