Skip to content
This repository was archived by the owner on Jun 26, 2022. It is now read-only.

Commit 900ed52

Browse files
Merge pull request #141 from Akash52/Akash52
Added code for Binary Exponantion in CPP
2 parents e5a552b + a23b59a commit 900ed52

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

cpp/Akash52_Binary_Exponentiation.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//Problem: Binary Exponentiation better approach
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
5+
int power(long long a,long b)
6+
{
7+
int result=1;
8+
while(b>0){ //Return 1
9+
if(b%2==1) result *=a; //Odd number
10+
a*=a;
11+
b/=2; //Even Number
12+
}
13+
return result;
14+
}
15+
int main()
16+
{
17+
long long a,b;
18+
cin>>a>>b;
19+
cout<<power(a,b);
20+
return 0;
21+
}
22+
23+
24+
//TimeCoplexity : O(logn)

0 commit comments

Comments
 (0)