-
Notifications
You must be signed in to change notification settings - Fork 33
/
power.cpp
50 lines (42 loc) · 895 Bytes
/
power.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
using namespace std;
int power(int x, int n);
int spower(int x, int n);
int main(){
int x,n,choice;
// Take input from user
cout<<"Enter base and power (separated by space): ";
cin>>x>>n;
// Take choice from user
cout<<"Press 1 for power(x,n) and 2 for spower(x,n): ";
cin>>choice;
switch(choice){
case 1: cout<<power(x,n);
break;
case 2: cout<<spower(x,n);
break;
default: break;
}
}
/*
power
-----
calculating x^n using recursion
*/
int power(int x,int n){
if(n==0){
return 1;
}
return x * power(x,n-1);
}
int spower(int x,int n){
if(n==0){
return 1;
}
int sp = spower(x, n/2);
int ans = sp * sp;
if(n%2==1){
ans = ans * x;
}
return ans;
}