File tree 1 file changed +54
-0
lines changed
1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ using namespace std ;
3
+
4
+ /* RUNTIME ERROR:
5
+ Last executed input:
6
+ 0.00001
7
+ 2147483647
8
+ |
9
+ V */
10
+ // double power(double x,int n){
11
+ // if(n==0){
12
+ // return 1;
13
+ // }
14
+ // if(x==0){
15
+ // return 0;
16
+ // }
17
+ // //recursive case
18
+ // if(n>0){ // Even and positive power
19
+ // return x*power(x,n-1);
20
+ // }
21
+ // else{ // Negative power
22
+ // n*=-1;
23
+ // return (1/(power(x,n)));
24
+ // }
25
+ // }
26
+
27
+ double power (double x,int n){
28
+ if (n==0 ){
29
+ return 1 ;
30
+ }
31
+ if (x==0 ){
32
+ return 0 ;
33
+ }
34
+ // recursive case
35
+ double temp=power (x,n/2 );
36
+
37
+ if (n%2 ==0 ){ // Even and positive power
38
+ return temp*temp;
39
+ }
40
+ else {
41
+ if (n>0 ){ // Odd and positive power
42
+ return x*temp*temp;
43
+ }
44
+ else // Negative power
45
+ n=abs (n);
46
+ return ((temp*temp)/x);
47
+ }
48
+ }
49
+ int main (){
50
+ double x;
51
+ int n;
52
+ cin>>x>>n;
53
+ cout<<power (x,n);
54
+ }
You can’t perform that action at this time.
0 commit comments