Write you own Power without using multiplication(*) and division(/) operators in C Program


Power function is calculated using the times multiple i.e. 5n is 5*5*5… n times. For this function, to work properly without using multiplication(*) and division(/) operators we will use nested loops that add the numbers n number of time.

Example

#include <iostream>
using namespace std;
int main() {
   int a= 4 , b = 2;
   if (b == 0)
      cout<<"The answer is"<<1;
   int answer = a;
   int increment = a;
   int i, j;
   for(i = 1; i < b; i++) {
      for(j = 1; j < a; j++) {
         answer += increment;
      }
      increment = answer;
   }
   cout<<"The answer is "<<answer;
   return 0;
}

Output

The answer is 16
raja
Published on 08-Aug-2019 11:14:49
Advertisements