multiply int..

This is a discussion on multiply int.. within the C Programming forums, part of the General Programming Boards category; How do I write the code for: int a, b; b = a^4 ; /* that doesn't work */ b ...

  1. #1
    Unregistered
    Guest

    multiply int..

    How do I write the code for:

    int a, b;

    b = a^4 ; /* that doesn't work */

    b = a * a * a; have I done so far...

    Is there a right way

    thank you all.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    here my code should help you...

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


    int main()

    {
    int x, y, i ;
    char yes;
    char var;
    int result = 1 ;

    again:

    printf("Please enter your calculation in this format: 6*5..\n\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 '^':


    {

    for(i=0; i<y; i++)

    result *=x;

    printf("answer = %d\n\n",result);

    break;


    default:

    printf("Wrong syntax please try again\n");
    }

    }
    }

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >b = a^4 ; /* that doesn't work */

    Well, that depends on what you want to do. The ^-operator is the XOR operation.

    >b = a * a * a; have I done so far...

    So you want to work with powers? You could use the following pseudocode.

    Code:
    pwr = 1;
    for i = 1 to n
        pwr = pwr * a;

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    there's the pow function in <math.h>

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    there's the pow function in <math.h>

    True. However it is slower than doing a*a*a*a.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 06:20 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21