Please use C language The software I use is codeblock so ple

(Please use C language)

(The software I use is codeblock so please make sure the code can run with out bugs)

(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!

The answer i GOT IS:

#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

the following photo is on my computer while I ran the code.

Solution

your code is right and giving the correct output for different inputs. save your file with name main.c and try to run on your compiler. you will get output.

main.c

#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 :

(Please use C language) (The software I use is codeblock so please make sure the code can run with out bugs) (Please write full code for me! Thank you) Write a
(Please use C language) (The software I use is codeblock so please make sure the code can run with out bugs) (Please write full code for me! Thank you) Write a
(Please use C language) (The software I use is codeblock so please make sure the code can run with out bugs) (Please write full code for me! Thank you) Write a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site