by CodeChum Admin
Functions that return the value of a base and an exponent is very common so I want to switch it up.
This time, we’re going to try to reverse-engineer these functions and find out the exponent of a certain number based on the value of the result and the base used to get that value.
Instructions:
Input
1. The base
2. The result
Output
Enter·the·base:·2
Enter·the·result:·8
Exponent·=·3
#include <iostream>
#include <cmath>
using namespace std;
int getExponent(int base, int result)
{
static int exp = 1;
static int base0 = base;
if (base != result)
{
exp++;
base *= base0;
return getExponent(base, result);
}
return exp;
}
int main()
{
int base, result;
cout << "Please, enter two integers: \n";
cout << "Enter the base: ";
cin >> base;
cout << "Enter the result: ";
cin >> result;
cout << "Exponent: " << getExponent(base, result);
}
Comments
Leave a comment