Originally posted by way87
C++ Program to Calculate X^Y
Collapse
X
-
#include <stdio.h>
#include <math.h>
int main (void)
{
double x;
double y;
double result;
printf("Enter an integer for x\n");
scanf("%d",&x);
printf("Enter an integer for y\n");
scanf("%d",&y);
result = pow(x,y);
printf("x raised to power y is %f\n",result);
return 0;
}
it not works,i input 8 as x and 2 as y,the output is 1...Comment
-
First, please use CODE tags unless your code is something like one function call or some obviously readable block. Be on the safe side as a beginner and always use them.
Next, why declare x and y as doubles if you treat them as integers in your program? Shouldn't you rather treat them as doubles? For example, your scanf format specifier would be %f, not %d, right? (You should be verifying this with a C reference that you can easily google. Or type in "man scanf" in Google).Comment
Comment