diff --git a/General/binary_exponentiation_iterative.cpp b/General/binary_exponentiation_iterative.cpp new file mode 100644 index 0000000..7bc381f --- /dev/null +++ b/General/binary_exponentiation_iterative.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; + +#define ll long long int + +int bexp(ll a,ll b){ + int res=1; //initializing result to 1 + while(b>0){ + if(b&1) + res*=a; //if b is odd then increasing res by a factor of a + a=a*a; //ans in all case squaring a to get value of a^2-->a^4-->a^8-->a^16 and so on + b=b/2; //dividing b by 2 + } + return res; +} + +int main(){ +ll a,b; //calculating a^b +cin>>a>>b; + +int ans=bexp(a,b); +cout<