# Thread: ODD or EVEN USING FOR LOOP

1. Member
Join Date
Sep 2013
Posts
1
Rep Power
0

## ODD or EVEN USING FOR LOOP

Good Day sir I'm Just a newbie in java i want to do a odd or even loop. but i got this problem and can't find what is wrong. can you help me i appreciate it alot. thanks here's my code

import java.util.Scanner;
public class practice1 {

public static void main(String[] args) {
String w;
int i;

Scanner In = new Scanner(System.in);

System.out.print("Enter a Number: ");w = In.nextLine();
String [] m = w.split("\\s+");
int [] a = new int[m.length];

for(i = 0;i<m.length;i++)
{
a[i] = Integer.parseInt(m[i]);
}
for(i = 0; i < a.length; i++)
if(a[i]%2==0){
System .out.println("Odd numbers are:" + i);
}
else if(a[i]%2!=0)
System .out.println("Even numbers are:" +i);

}
}

the output of this code is like this:
Enter a number: 1 2
Odd numbers are : 1
Even numbers are : 1

2. Moderator
Join Date
Apr 2009
Posts
13,064
Rep Power
23

## Re: ODD or EVEN USING FOR LOOP

Moving to New to Java from a staff only section.

When posting code please use [code] tags [/code].

3. Senior Member
Join Date
Jan 2009
Location
CA, USA
Posts
271
Rep Power
8

## Re: ODD or EVEN USING FOR LOOP

Try this for the last for loop
Java Code:
```for(i = 0; i < a.length; i++)
{
if(a[i] % 2 == 0)
{
System.out.println(a[i] + " EVEN");
}
else
{
System.out.println(a[i] + " ODD");
}
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•