Thread: Find the number of even numbers on even positions

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    1

    Question Find the number of even numbers on even positions

    Display the number of even numbers on even positions in the sequence.

    Input
    5
    1 2 4 5 6



    Output
    2

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You're not expecting us to do your assignment for you, are you? Because we won't.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    What are you having problems with?
    Just iterate through the indices 0, 2, 4, 6... and check if the number at this position is even or not (using modulo).

    You need 2 variables - counter and position. If position is odd, ignore current number, otherwise check if the current number is even and increment counter.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    87
    Quote Originally Posted by kmdv View Post
    What are you having problems with?
    Just iterate through the indices 0, 2, 4, 6... and check if the number at this position is even or not (using modulo).

    You need 2 variables - counter and position. If position is odd, ignore current number, otherwise check if the current number is even and increment counter.
    its easier to input them into an array, then move through them with a double incrementing for loop
    Code:
    for(int a = 0; a < array_end; a+=2)

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by bobknows View Post
    its easier to input them into an array, then move through them with a double incrementing for loop
    Code:
    for(int a = 0; a < array_end; a+=2)
    I wouldn't say it's easier, he probably doesn't know how to create a dynamic array. Moreover it would needlessly consume memory.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    87
    true,

    i guess i was only thinking of how short to get it, not how complex it would be.
    i didnt think of having one variable and cin ing it multiple times either.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number user input
    By jimtuv in forum C Programming
    Replies: 62
    Last Post: 06-13-2010, 11:08 AM
  2. Multiple classes question
    By TriKri in forum C++ Programming
    Replies: 20
    Last Post: 06-11-2010, 04:03 PM
  3. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  4. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  5. Find all factors of a number
    By Spono in forum C Programming
    Replies: 5
    Last Post: 10-23-2003, 01:23 AM