Advertisement
Guest User

Untitled

a guest
Dec 27th, 2013
28
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int power(int n, int k) {
  2.   if (k == 0)
  3.     return 1;
  4.   else if (k % 2) // k = 2m + 1, m ≥ 0
  5.     return n * power(n, k - 1);
  6.   else // k = 2m, m > 0
  7.     return power(n * n, k / 2);
  8. }
  9.  
  10. void task2() {
  11.   int n, k;
  12.  
  13.   cout << "Input n: "; cin >> n;
  14.   cout << "Input k: "; cin >> k;
  15.  
  16.   if (k < 0)
  17.     cout << "k < 0";
  18.   else
  19.     cout << "n^k = " << power(n, k);
  20.  
  21.   cout << endl;
  22. }
Advertisement
RAW Paste Data Copied
Advertisement