(Please use C language) (Please write full code for me! Thank you) Write a funct
ID: 3929733 • Letter: #
Question
(Please use C language)
(Please write full code for me! Thank you)
Write a function called power that returns the value of baseexponent as type double. The base is type double and the exponent is type int which can have negative, zero, or positive values.
Hints:
• Prompt the user to enter two numbers, one as the base and one as the exponent. You should prompt the user that the exponent should be an integer number.
• While the return value is type double, if both base and exponent are integer numbers the program should print an integer number.
• If the base is zero, the program should return 0 as the result.
• If the exponent is zero, the program should return 1 as the result.
Note: You cannot use math functions.
Thank you!
Explanation / Answer
#include<stdio.h>
double power(double base,int exponent)
{
if(base==0)
return 0;
if(exponent == 0)
return 1;
double result = 1;
int i;
if(exponent>0)
{
for(i=1;i<=exponent;i++)
result = result*base;
}
else
{
for(i=exponent;i<0;i++)
result = result*(1/base);
}
return result;
}
int main()
{
double base;
int exponent;
printf("Input the base : ");
scanf("%lf",&base);
printf("Input the exponent(Integer): ");
scanf("%d",&exponent);
double base_exponent = power(base,exponent);
printf("Result of %lf raise to the power of %d is %lf",base,exponent,base_exponent);
}
OUTPUT:
Input the base : 10
Input the exponent(Integer): -2
Result of 10.000000 raise to the power of -2 is 0.010000
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.