Write a program power that computes x raised to the power y fot integers x and y and returns double-typ value.
#include <stdio.h>
void main( )
{ int x,y; /*input data */
double power(int, int); /* prototype declaration*/
printf("Enter x,y:");
scanf("%d %d" , &x,&y);
printf("%d to power %d is %f\n", x,y,power (x,y));
getch();
}
double power (int x, int y)
{
double p;
p = 1.0 ; /* x to power zero */
if(y >=0)
while(y--) /* computes positive powers */
p *= x;
else
while (y++) /* computes negative powers */
p /= x;
return(p); /* returns double type */
}
OUTPUT
Enter x,y : 16 2
16 to power 2 is 256.000000
Enter x,y : 16 -2
16 to power -2 is 0.003906
#include <stdio.h>
void main( )
{ int x,y; /*input data */
double power(int, int); /* prototype declaration*/
printf("Enter x,y:");
scanf("%d %d" , &x,&y);
printf("%d to power %d is %f\n", x,y,power (x,y));
getch();
}
double power (int x, int y)
{
double p;
p = 1.0 ; /* x to power zero */
if(y >=0)
while(y--) /* computes positive powers */
p *= x;
else
while (y++) /* computes negative powers */
p /= x;
return(p); /* returns double type */
}
OUTPUT
Enter x,y : 16 2
16 to power 2 is 256.000000
Enter x,y : 16 -2
16 to power -2 is 0.003906
There is some mistake in your code.
ReplyDelete