C++ Program to Calculate X^Y

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #16
    Originally posted by way87
    it should not be correct..
    becoz the output should be:
    1
    4
    9
    16
    25
    36
    49
    64
    81
    100

    ?
    No, not the way you have it written. You do nothing to 'y' through the whole thing. So it is always 1. You need to increment y.

    Comment

    • way87
      New Member
      • Sep 2007
      • 8

      #17
      im really stuck,i dunno how to fix it

      Comment

      • way87
        New Member
        • Sep 2007
        • 8

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

        • oler1s
          Recognized Expert Contributor
          • Aug 2007
          • 671

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

          Working...