• Home
  • Genetik
  • Google
  • Links
  • About
  • Yohan Naftali

    Even or Odd?

    Oct. 20, 2009

    There are many ways to determine wheter the number is odd or even. Here is two:
    1. Check with modulus operator (%)
    The modulus operator returns the remainder when dividing two numbers.
    Even numbers doesn’t have a remainder, but Odd numbers have 1.
    2. Check by bit operators

    Here is the code:
    1. With modulus operator (%)
    ———————————-
    #include
    int main() {
    int number;
    printf(”Input integer\n”);
    scanf(”%d”,&number);
    if(number % 2 == 0) {
    printf(”Even\n”);
    }
    else {
    printf(”Odd\n”);
    }
    return 0;
    }
    ———————————-

    2. Check by bit operator
    ———————————-
    #include
    int main() {
    int number;
    printf(”Input integer\n”);
    scanf(”%d”,&number);
    if(number & 1) {
    printf(”Odd\n”);
    }
    else {
    printf(”Even\n”);
    }
    return 0;
    }
    ———————————-


    Share on Facebook Delicious Bookmark this on Delicious

    Leave a Reply