Advertisement
Niloy007

Musa's problem's solution recursive

Jun 6th, 2020
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.32 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int pow(int b, int p) {
  4.     if (p == 1) {
  5.         return 2;
  6.     }
  7.     int result = 1;
  8.     if (p % 2 == 0) {
  9.         p /= 2;
  10.         result = pow(b, p);
  11.         result *= result;
  12.        
  13.     } else {
  14.         result = pow(b, p - 1);
  15.         result *= b;
  16.     }
  17.  
  18.     return result;
  19. }
  20.  
  21. int main() {
  22.     int n = 2, k = 30;
  23.     printf("%d\n", pow(n, k));
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement