# ODD or EVEN USING FOR LOOP

• 09-28-2013, 04:28 PM
jellybeanjo
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
• 09-30-2013, 11:17 AM
Tolls
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].
• 09-30-2013, 03:18 PM
AndrewM16921
Re: ODD or EVEN USING FOR LOOP
Try this for the last for loop
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");         } }```