What i gotta do:
Write a function called "integerPower(base, exponent)" that calculates and then returns the value of base to the power exponent. For example, if you invoke the function in the following manner:
integerPower(3,4);
the function should return the value 81 (3 * 3 * 3 * 3 = 81). Assume that "exponent" is a positive nonzero integer and that "base" is an integer. You should use type "long" to represent the exponent and base. The function should return a "long" value as well. Do NOT use any math library functions as part of your solution.
Write a main() function to use (and test) your integerPower() function. Your main() function should request a base and exponent value from the user and then invoke the function with the values provided by the user.
As a general rule, it is prefered that you NOT input or output (scanf or printf) from inside a function. By having the calling program (main() in our case) do all the input and output, your function remains more general purpose.
What i got .... cant seem to get the function workin properly
Code:
#include <stdio.h>
long integerpower( long base, long exponent );
int main()
{
long base;
long exponent;
printf("\nEnter the base value: ");
scanf("%d", &base);
do{
printf("Enter the exponent value(must be a positive integer): " );
scanf("%d", &exponent);
if(exponent<0){
printf("\nInvalid data entered, try again!\n" );
}
} while(exponent<0);
printf("%d raised to the power %d is: %d\n\n", base, exponent , integerpower);
return 0;
}
long integerpower(long base, long exponent)
{
if (exponent == 1)
return base;
else
return(base * integerpower(base, exponent -1));
}