#include <iostream>
using namespace std;
int countMultiplications(int n) {
if (n == 0 || n == 1) {
return 0;
}
if (n % 2 == 0) {
return 1 + countMultiplications(n / 2);
} else {
return 2 + countMultiplications((n - 1) / 2);
}
}
int main() {
int n;
cout<<"Enter The value of exponent: ";
cin>>n;
int multiplications = countMultiplications(n);
cout << "Number of multiplications needed for x^" << n << " is: " << multiplications << endl;
return 0;
}
Comments
Post a Comment