# Help With Odd/Even number program

• November 23rd, 2009, 08:25 AM
JonoScho
Help With Odd/Even number program
Hey guys i'm new to java and im trying to teach myself java to help with my university course. Im trying to create a program that takes in a integer and returns the odd and even values upto that number. Here is the code i have at the moment and i would appreciate if someone could explain to me where im going thanks in advance.

Code :

```  import java.util.*;   public class OddEvenNum { public static void main(String[] args)   { int i;   char choice; Scanner sc = new Scanner(System.in);   do { System.out.println("Welcome to odd and even number checker please enter a number"); i = sc.nextInt();       if(i%2 == 0; i++) { System.out.println("Even number is :" + i); } else { System.out.println("Odd number is :" + i); }     System.out.println("Would you like to try another number? :"); choice = sc.next() .charAt(0);           }while (choice == 'y' || choice == 'Y'); } }```
• November 23rd, 2009, 08:52 AM
chronoz13
Re: Help With Odd/Even number program
hmm looks like your program satisfies what you need.

• November 23rd, 2009, 08:58 AM
JonoScho
Re: Help With Odd/Even number program
Basically it takes in the integer then only returns the first odd or even number i want it to display all the odd and even numbers for example:
Int 10
odd number is 1
even number is 2
odd number is 3
even number is 4
all the way to 10 etc
• November 23rd, 2009, 09:39 AM
copeg
Re: Help With Odd/Even number program
You need to include a loop

Code :

```for ( int j = 0; j < i; j++ ){ //check j for even or odd and act accordingly }```
• November 23rd, 2009, 09:56 AM
JonoScho
Re: Help With Odd/Even number program
thats got me one step further but it will now print the number the amount of times of the integer e.g. it prints 5, 5 times

Code :

```import java.util.*;   public class Test { public static void main(String[] args)   { int i;   char choice; Scanner sc = new Scanner(System.in);   do { System.out.println("Welcome to odd and even number checker please enter a number"); i = sc.nextInt();   for(int j = 0; j < i; j++) {   if(i%2 == 0) { System.out.println("Even number is :" + i); } else { System.out.println("Odd number is :" + i); }   } System.out.println("Would you like to try another number? :"); choice = sc.next() .charAt(0);           }while (choice == 'y' || choice == 'Y'); } }```
• November 23rd, 2009, 10:07 AM
chronoz13
Re: Help With Odd/Even number program
something like this ?

Code :

``` public static void main(String[] args) {   int count = 0;   do {   count++;   if (count % 2 == 0) {   System.out.println(count + " is even"); } else {   System.out.println(count + " is odd");   } } while(count != 10);   } }```
• November 23rd, 2009, 10:13 AM
JonoScho
Re: Help With Odd/Even number program
ye only i want it to display all the odd numbers or even numbers depending on the value i enter
• November 23rd, 2009, 10:53 AM
chronoz13
Re: Help With Odd/Even number program
this one?
Code :

``` public static void main(String[] args) throws IOException {   int count = 0;   System.out.print("Enter The Max Value: "); int max = Integer.parseInt(br.readLine());   System.out.print("Which Value Do You Want To Display Even Or Odd?"); String answer = br.readLine();   do {   count++;   if (answer.equalsIgnoreCase("even")) {   if (count % 2 == 0) {   System.out.println(count); } } else if (answer.equalsIgnoreCase("odd")) {   if (count % 2 == 1) {   System.out.println(count); } } } while (count != max); } }```