Thread: ^power

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    227

    ^power

    i have created a simple calc but how can i make it calculate
    3^5 using switch and case... well anywayz just in case here is my code

    #include <stdio.h>
    #include <math.h>

    int main()

    {
    int x,y;
    char var;



    printf("Type the operator +,-,/,* and your prob in this format 5+5\n");
    scanf("%d%c%d",&x,&var,&y);
    switch(var)
    {
    case '+':
    printf("Answer = %d\n",x+y);
    break;
    case '/':
    printf("Answer = %d\n", x/y);
    break;
    case '-':
    printf("Answer = %d\n", x-y);
    break;
    case '*':
    printf("Answer = %d\n", x*y);
    break;
    case '^':
    printf("Answer = %d\n", x^y);
    break;
    default:
    printf(" please try that again\n ");
    system("./a.out");
    }
    }

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Look up the pow() function in the help.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    the calc is being created in linux.. sorry..

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    reply

    This is probably not an efficient way to do this, but anyway:
    Code:
    int Result=1;
    for(int i=0; i<y; i++)
    {
      Result*=x;
    }
    Assuming x and y is the same as in your code, and y is never less than 0.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    ya i need it with case... if i do it like that then i will have to redo my code in somewayz

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>>
    if i do it like that then i will have to redo my code in somewayz
    <<<

    Your going to have to redo it anyway as what you've got doesn't work!

    If you haven't got a power function, (I can't believe Linux does not have a power function, I don't know what it would be called, but it is there somewhere), then you're going to have to calculate it aren't you.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by xlordt
    ya i need it with case... if i do it like that then i will have to redo my code in somewayz
    Not that much:
    Code:
    case '^':
      for(int i=0; i<y; i++)
      {
        Result*=x;
      }
      printf("Answer = %d\n", Result);
    break;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    i keep getting a pars error in int... how can i fix thiz

  9. #9
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    That's because in C, you have to declare your variables before everything else. Put the int i = 0; with your other variable declarations.

Popular pages Recent additions subscribe to a feed