find even or odd in array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AsHantoosH
    New Member
    • Feb 2009
    • 7

    find even or odd in array

    #include<stdio. h>

    main() {

    int a;

    printf("Enter a: \n");

    scanf("%d", &a);


    if (a % 2 == 0) {

    printf("The given number is EVEN");

    } else {

    printf("The given number is ODD");

    }

    }

    the above program find the even or odd but how can i find multiple of 2 for array of 100 elemens
  • scruggsy
    New Member
    • Mar 2007
    • 147

    #2
    Originally posted by AsHantoosH
    #include<stdio. h>
    the above program find the even or odd but how can i find multiple of 2 for array of 100 elemens
    Any even number is a multiple of 2. No odd number is. So it looks like you already have most of the code to do this, you just need to do it inside a loop inspecting the elements of your array one at a time. If that's the part you're having trouble with, post what you've tried.

    As a somewhat useless aside, another way to test if an integer is odd or even:
    Code:
    if (a & 1)  // odd
    else   // even

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Originally posted by AsHantoosH
      the above program find the even or odd but how can i find multiple of 2 for array of 100 elemens
      By "multiple of 2", do you mean "even" (2, 4, 6, 8, 10, ...) or do you mean "power of two" (2, 4, 8, 16, 32, ...)?

      Comment

      • AsHantoosH
        New Member
        • Feb 2009
        • 7

        #4
        multiple of 2

        i mean multiple of 2 (2,4,6)

        Comment

        • Meetee
          Recognized Expert Contributor
          • Dec 2006
          • 928

          #5
          In your even number logic you can extends the logic for the multiplicity of 2 as every even number except 0. So modify your logic accordingly.

          Regards

          Comment

          • scruggsy
            New Member
            • Mar 2007
            • 147

            #6
            Originally posted by zodilla58
            In your even number logic you can extends the logic for the multiplicity of 2 as every even number except 0. So modify your logic accordingly.

            Regards
            Not to be nitpicky (or rather, to be very nitpicky :p) but zero is a multiple of all numbers but a factor of none. Negative even numbers are also multiples of 2. Whether either applies in this case depends on the problem statement I guess - OP doesn't specify.

            Comment

            • rahulthapar07
              New Member
              • Mar 2009
              • 1

              #7
              [ incorrect spoonfeeding code deleted ]

              Jos (moderator)

              Comment

              Working...