Calculate power of an element(x,y) where x is base and y is power in O(log(y)) times.with O(1) space complexity without using DIVIDE & Conquer !!

Calculate the power of an element(x,y) where x is base and y is power in O(log(y)) times. with O(1) space complexity without using DIVIDE & Conquer !!
                                                                                                                                                                                         
                                                 C++ program for the above approach 
Follow me on :

Instagram        Twitter           LinkedIn             my website                facebook          Whatsapp             gmail


#include <bits/stdc++.h> 

using namespace std;

 int main ()
 { 
        int x = 2;                  //base 
        int y = 5;                 //power i.e x^y 
        int s = x; 

     for (int i = 1; i <= int (log2 (y)); i++) 
         {
                s = s*s;
          }
                  if(y % 2 == 0)
                        cout << x; 
                   else 
                         cout << x * s;

 }
// code end//
       // This is code is contributed //
       //    by Ayushshuklas 
                                                                   
Linkedin 
                                                      my website : ayushshuklas.netlify.com

Comments

Popular posts from this blog

LCM code tech mahindra 2022

Consultancy company product id codeof two list in wipro feb 2022

Sum of largest and smallest element in array by tech mahindra 2022