Guest User

Untitled

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