# Counting all even numbers and odd numbers in a given arrary

Page 1 of 1

## 11 Replies - 954 Views - Last Post: 15 December 2012 - 07:51 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=304044&amp;s=8a9b0d603262b9a6e347a7728fb12c53&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 stvo131

Reputation: 0
• Posts: 7
• Joined: 15-December 12

# Counting all even numbers and odd numbers in a given arrary

Posted 15 December 2012 - 12:49 PM

```/**

* S.M.
* 12-15-12
*/
public class arrarys
{

public static void one()
{
int countEvens=0;
int countOdds=0;

int[] numbs= new int []{2, 4, 6, 8, 10, 12, 14, 19, 21, 47};
for(int index=0;index<numbs.length;index++){
if(index%2 == 0) {
countEvens++;
}
else {
countOdds++;
}
}

System.out.println("The number of even numbers is" + countEvens);
System.out.println("The number of odd numbers is" + countOdds);
}}

```

I know that if I use
``` for(int index : numbs)
```

I can get my code to work, but I want to know why the way I have it now as
```for(int index=0;index<numbs.length;index++)
```
does not work. Thanks!

Is This A Good Question/Topic? 0

## Replies To: Counting all even numbers and odd numbers in a given arrary

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12226
• Posts: 45,301
• Joined: 27-December 08

## Re: Counting all even numbers and odd numbers in a given arrary

Posted 15 December 2012 - 12:53 PM

In the enhanced for loop, the term index takes on the value of an element in the array. This is not the case in a regular for loop. You would have to use the syntax numbs[index] to access a specific element in the array.

### #3 CasiOo

• D.I.C Lover

Reputation: 1575
• Posts: 3,541
• Joined: 05-April 11

## Re: Counting all even numbers and odd numbers in a given arrary

Posted 15 December 2012 - 12:55 PM

```if(index%2 == 0) {

```

You are checking if index is even or odd instead of checking the number in the array

You can lookup values in the array by doing numbs[<some integer here>]
```numbs[0]; //Will be 2
numbs[1]; //Will be 4
numbs[2]; //Will be 6
...

```

### #4 stvo131

Reputation: 0
• Posts: 7
• Joined: 15-December 12

## Re: Counting all even numbers and odd numbers in a given arrary

Posted 15 December 2012 - 12:59 PM

Oh wait why didn't I see it before what I have isn't reading the array it's just repeating 9 times....

Thank's guys. I'll tinker with it a bit and see what I get.

### #5 k3y

Reputation: 36
• Posts: 205
• Joined: 25-February 12

## Re: Counting all even numbers and odd numbers in a given arrary

Posted 15 December 2012 - 05:21 PM

FOR-EACH: Is based on elements, and
FOR: are based on index
```public class ArraysPractice
{
public static void main(String[] args)
{
int evenNums = 0;
int oddNums = 0;

int[] nums = {2, 4, 6, 8, 10, 12, 14, 19, 21, 47}; // no need to include new int [] , becuase {value1, value2, etc} determine the number of elements and allocates the mem

for(int index = 0; index < nums.length; index++) // start at index 0
{
if(nums[index] % 2 == 0)
{
evenNums++;
}
else
{
oddNums++;
}
}

/*for(int element : nums) // the for-each loop is based on elements, the for loop is based on index
{
if(element % 2 == 0)
{
evenNums++;
}
else
{
oddNums++;
}
}
*/
System.out.println("even numbers total: " + evenNums);
//7
System.out.print("odd numbers totoal: " + oddNums);
//3
}
}

```

### #6 stvo131

Reputation: 0
• Posts: 7
• Joined: 15-December 12

## Re: Counting all even numbers and odd numbers in a given arrary

Posted 15 December 2012 - 06:04 PM

I ended up just doing this, but thank you guys.

```public static void one()
{
int countEvens=0;
int countOdds=0;

int[] numbs= new int []{2, 4, 6, 8, 10, 12, 14, 19, 21, 47};
//for(int index=0;index<numbs.length;index++){
if
(numbs[0]%2 == 0)

{
countEvens++;
}
else {
countOdds++;

}
if
(numbs[1]%2 == 0)

{
countEvens++;
}
else {
countOdds++;

}

if
(numbs[2]%2 == 0)

{
countEvens++;
}
else {
countOdds++;

}
if
(numbs[3]%2 == 0)

{
countEvens++;
}
else {
countOdds++;

}
if
(numbs[4]%2 == 0)

{
countEvens++;
}
else {
countOdds++;

}

if
(numbs[5]%2 == 0)

{
countEvens++;
}
else {
countOdds++;

}
if
(numbs[6]%2 == 0)

{
countEvens++;
}
else {
countOdds++;

}

if
(numbs[7]%2 == 0)

{
countEvens++;
}
else {
countOdds++;

}
if
(numbs[8]%2 == 0)

{
countEvens++;
}
else {
countOdds++;

}
if
(numbs[9]%2 == 0)

{
countEvens++;
}
else {
countOdds++;

}
System.out.println("The number of even numbers is" + countEvens);
System.out.println("The number of odd numbers is" + countOdds);
}}
```

### #7 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Counting all even numbers and odd numbers in a given arrary

Posted 15 December 2012 - 06:14 PM

Why ???
Why not using a for() loop ?
What will you do when your array will have 25000 elements ???

### #8 k3y

Reputation: 36
• Posts: 205
• Joined: 25-February 12

## Re: Counting all even numbers and odd numbers in a given arrary

Posted 15 December 2012 - 07:04 PM

stvo131, on 15 December 2012 - 08:04 PM, said:

I ended up just doing this, but thank you guys.

```public static void one()
{
int countEvens=0;
int countOdds=0;

int[] numbs= new int []{2, 4, 6, 8, 10, 12, 14, 19, 21, 47};
//for(int index=0;index<numbs.length;index++){
if
(numbs[0]%2 == 0)

{
countEvens++;
}
else {
countOdds++;

}
if
(numbs[1]%2 == 0)

{
countEvens++;
}
else {
countOdds++;

}

if
(numbs[2]%2 == 0)

{
countEvens++;
}
else {
countOdds++;

}
if
(numbs[3]%2 == 0)

{
countEvens++;
}
else {
countOdds++;

}
if
(numbs[4]%2 == 0)

{
countEvens++;
}
else {
countOdds++;

}

if
(numbs[5]%2 == 0)

{
countEvens++;
}
else {
countOdds++;

}
if
(numbs[6]%2 == 0)

{
countEvens++;
}
else {
countOdds++;

}

if
(numbs[7]%2 == 0)

{
countEvens++;
}
else {
countOdds++;

}
if
(numbs[8]%2 == 0)

{
countEvens++;
}
else {
countOdds++;

}
if
(numbs[9]%2 == 0)

{
countEvens++;
}
else {
countOdds++;

}
System.out.println("The number of even numbers is" + countEvens);
System.out.println("The number of odd numbers is" + countOdds);
}}
```

no offense, but that is extremely inefficient code you are rocking there. I'd suggest using either a for or a for-each loop, like I suggested.

### #9 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12226
• Posts: 45,301
• Joined: 27-December 08

## Re: Counting all even numbers and odd numbers in a given arrary

Posted 15 December 2012 - 07:09 PM

You should also look into a legible indentation and spacing convention set. Your code is horrid to read, no offense intended.

### #10 k3y

Reputation: 36
• Posts: 205
• Joined: 25-February 12

## Re: Counting all even numbers and odd numbers in a given arrary

Posted 15 December 2012 - 07:10 PM

macosxnerd101, on 15 December 2012 - 09:09 PM, said:

You should also look into a legible indentation and spacing convention set. Your code is horrid to read, no offense intended.

That's what copy + pasting does.

### #11 stvo131

Reputation: 0
• Posts: 7
• Joined: 15-December 12

## Re: Counting all even numbers and odd numbers in a given arrary

Posted 15 December 2012 - 07:31 PM

yeah, I kno that a for loop or a for-each loop is much, much more effective, but this is what my teacher wants. A kid in my class asked the same thing: "What if our arrary has 30000 elements in it?"

My teacher responded that we will go over how to do it without copying and pasting next week.

### #12 CasiOo

• D.I.C Lover

Reputation: 1575
• Posts: 3,541
• Joined: 05-April 11

## Re: Counting all even numbers and odd numbers in a given arrary

Posted 15 December 2012 - 07:51 PM

teaching arrays before loops? why would anyone do that