In response to this prompt:
Write a function
integerPower( base, exponent )that returns the value of base exponent. For example,integerPower( 3, 4 ) == 3 * 3 * 3 * 3. Assume thatexponentis a positive, nonzero integer andbaseis an integer. The functionintegerPowershould usefororwhileto control the calculation. Do not use any math library functions.
I wrote this program:
#include <iostream>
#include <conio.h>
using namespace std;
int integer (int a, int b) {
int e;
for (int i = 1; i <= b; i++)
e *= a;
return (e);
}
int main () {
cout << "enter number and exponent";
cin >> num >> exp;
cout << num << "to the power" << exp << "is" <<;
int num, exp, n;
integer (num, exp, n);
getch ();
return 0;
}
For some reason the function integer (int a, int b) returns 0 no matter what the values of a and b are. Why?
Inside the function
integer, initializeelike this :Also, move
int num, exp, n;before thecinstatement.