Results 1 to 3 of 3
  1. #1
    jellybeanjo is offline Member
    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Default 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. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default 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].
    Please do not ask for code as refusal often offends.

  3. #3
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    261
    Rep Power
    6

    Default 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");
    	}
    }

Similar Threads

  1. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  2. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  3. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  4. Convert do while loop to for loop
    By sandeeptheviper in forum New To Java
    Replies: 3
    Last Post: 01-03-2011, 12:37 PM
  5. A loop that doesn't loop
    By MichYer in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 08:44 AM

Posting Permissions

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