GATE | GATE CS 2020 | Question 58

Consider the following C functions.

filter_none

edit
close

play_arrow

link
brightness_4
code

int tob (int b, int* arr) {
    int i;
    for (i = 0; b>0; i++)  {
        if (b%2)  arr [i] = 1;
        else      arr[i] = 0;
        b = b/2;
    }
    return (i);
}
   
  
int pp(int a, int b)  {
    int  arr[20];
    int i, tot = 1, ex, len;
    ex = a;
    len = tob(b, arr);
    for (i=0; i<len ; i++) {
         if (arr[i] ==1)
             tot = tot * ex;
         ex= ex*ex;
    }
return (tot) ;
}

chevron_right


The value returned by pp(3,4) is ________ .

Note – This question was Numerical Type.
(A) 81
(B) 64
(C) 100
(D) 49


Answer: (A)

Explanation: Given functions compute power of an elements, so it will print 3^4 = 81.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h> 
  
int tob (int b, int* arr);
int pp(int a, int b);
  
int tob (int b, int* arr) {
    int i;
    for (i = 0; b>0; i++)  {
        if (b%2)  arr [i] = 1;
        else      arr[i] = 0;
        b = b/2;
    }
    return (i);
}
   
  
int pp(int a, int b)  {
    int  arr[20];
    int i, tot = 1, ex, len;
    ex = a;
    len = tob(b, arr);
    for (i=0; i<len ; i++) {
         if (arr[i] ==1)
             tot = tot * ex;
         ex= ex*ex;
    }
return (tot) ;
}
  
  
int main() 
 
     
  printf("%d", pp(3, 4)); 
  return 0; 
 

chevron_right


Code – https://ide.geeksforgeeks.org/snkT2QVCkm

Quiz of this Question




My Personal Notes arrow_drop_up
Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.