I am trying to write a program that solves exponents without using the pow () function. I think I need to use a loop to generate how many times the base multiplies itself as long as the exponent is positive. Any ideas?
#include <iostream>
usingnamespace std;
int main()
{
double n = 5; // We want 5^4
int e = 4;
double T = 1;
for(int k=1; k<=e; k++)
T = T*n;
cout << T << endl;
return 0;
}
#include <iostream>
usingnamespace std;
int main()
{
double n = 5; // We want 5^4
int e = -4;
double T = 1;
for(int k=1; k<=abs(e); k++)
T = T*n;
T = (e<0) ? 1/T : T;
cout << T << endl;
return 0;
}
Thanks a lot! I really appreciate you two taking the time to explain this to a noobie. This is how I can hopefully get to your knowledge base some day!