Re : Logic problem

This is a discussion on Re : Logic problem within the C Programming forums, part of the General Programming Boards category; Dear All, I don't understand why the following function is wrong, can anyone tell me : void check(int a) { ...

  1. #1
    Wah
    Wah is offline
    Registered User
    Join Date
    Jan 2002
    Posts
    5

    Re : Logic problem

    Dear All,

    I don't understand why the following function is wrong, can anyone tell me :

    void check(int a) {
    if (a / 2 == 1)
    printf("a is odd\n");
    else
    printf("a is even\n");
    }

    Thanks a million.

    Best regards,
    Wah

  2. #2
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    If you think about it carefully, a number / 2 == 1 is not working. What if the number is 11 ? 11 / 2 = 5. I think you wanna do somethin like this:
    Code:
    checkEven( int a )
    {
       if ( a % 2 == 0 )
          return 1;
       else
          return 0;
    }

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    void check(int a) 
    { 
      if (a % 2 == 1) 
        printf("a is odd\n"); 
      else 
        printf("a is even\n"); 
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    isn't that the same as my one lol?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Yes, but we posted at approximately the same time.

    -Prelude
    My best code is written with the delete key.

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >void check(int a) {
    >if (a / 2 == 1)
    >printf("a is odd\n");
    >else
    >printf("a is even\n");
    >}

    A logical answer why this is wrong:

    a / 2 = 1 if and only if a = 2

    By the way, in your function 2 is odd. :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logic
    By LordBronz in forum C++ Programming
    Replies: 6
    Last Post: 05-23-2006, 05:41 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  4. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

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