# Thread: Odd and Even Numbers

1. Member
Join Date
Apr 2009
Posts
84
Rep Power
0

## Odd and Even Numbers

Ive faced a problem here ,
user enters some numbers , programs shows how many of them are odd and how many are even.
here is what i have so far :

Java Code:
```import java.util.Scanner;

public class OddEven {
public static void main (String [] args){
int evenCount=0;int oddCount=0;
Scanner sc=new Scanner(System.in);
System.out.print("enter 10 numbers : ");

int input=sc.nextInt();
for(int i=1;i<input;i++){

if (i%input==0){
evenCount++;

}
else {
oddCount++;
}

}

System.out.print("u have entered "+evenCount+" even numbers");

}

}```
each time i run the program (testing only even numbers) , i get 0. is there something wrong with my counter?

2. ## Re: Odd and Even Numbers

Where do you change the value of the the variable: input inside of the loop?

Rethink your logic to get a new input to test.

How many numbers were you able to enter for the program to read when you executed the program?
This should have told you there was a logic problem.

3. Member
Join Date
Apr 2009
Posts
84
Rep Power
0

## Re: Odd and Even Numbers

Originally Posted by Norm
Where do you change the value of the the variable: input inside of the loop?

Rethink your logic to get a new input to test.

How many numbers were you able to enter for the program to read when you executed the program?
This should have told you there was a logic problem.
lets say im trying to enter following numbers : 10 20 25 23 44 8 3 7 39 56 . without having to ask user to enter each number separately ,ive modified the code into this :

Java Code:
``` public class OddEven {
public static void main (String [] args){
int evenCount=0;int oddCount=0;
Scanner sc=new Scanner(System.in);
System.out.print("enter 10 numbers : ");

int input=sc.nextInt();
for(int i=1;i<50;i++){

if (i%input==0){
evenCount++;

}
else {
oddCount++;
}

}

System.out.print("u have entered "+evenCount+" even numbers");

}

}```
but the output is not the exact count of even numbers.
Last edited by pinkdreammsss; 04-13-2012 at 01:40 PM.

4. ## Re: Odd and Even Numbers

Where do you change the value of the variable: input inside of the loop?

Add a println statement inside of the loop immediately after the for statement (line 9) that prints out the value of the input variable so you can see what the computer sees when it executes the code.

5. ## Re: Odd and Even Numbers

A question for you: why do you loop fifty times? And why do you only read a number once? I don't understand your program logic and I think it is completely wrong.

kind regards,

Jos

6. Member
Join Date
Apr 2009
Posts
84
Rep Power
0

## Re: Odd and Even Numbers

Originally Posted by JosAH
A question for you: why do you loop fifty times? And why do you only read a number once? I don't understand your program logic and I think it is completely wrong.

kind regards,

Jos
for the loop, i know the problem is the logic , my loop should generate a range of numbers, say from 1 to 100, so :if x%rangeOfnumbers=0 then count++; but the loop im using is not doing so.

also, user enters all the numbers at once so program wouldnt have to prompt for user input multiple times. e.g: 2 5 7 9 10 38. notice the space between each number.
Last edited by pinkdreammsss; 04-13-2012 at 03:16 PM.

7. AN21XX
Join Date
Mar 2012
Location
Munich
Posts
297
Rep Power
6

## Re: Odd and Even Numbers

That won't work... you need to split your input first for having separate numbers... try e.g. the split() function of the String class and loop through the resulting array of single numbers the user entered. ;)
Like that you try this: if ( 1 % "1 7 9 8" == 0)... 1 dividefd by a generic String won't work very well...

8. Member
Join Date
Apr 2009
Posts
84
Rep Power
0

## Re: Odd and Even Numbers

Originally Posted by Sierra
That won't work... you need to split your input first for having separate numbers... try e.g. the split() function of the String class and loop through the resulting array of single numbers the user entered. ;)
Like that you try this: if ( 1 % "1 7 9 8" == 0)... 1 dividefd by a generic String won't work very well...
thanks for the tip , tho im trying to do this without using arrays.

9. ## Re: Odd and Even Numbers

The call to nextInt() should work with all the numbers entered on one line. However if you do not call nextInt inside the loop you will not get the next number.

10. AN21XX
Join Date
Mar 2012
Location
Munich
Posts
297
Rep Power
6

## Re: Odd and Even Numbers

Sorry, my fault, Norm, you are right... you just need it inside the loop. :)

11. Member
Join Date
Apr 2009
Posts
84
Rep Power
0

## Re: Odd and Even Numbers

Originally Posted by Norm
The call to nextInt() should work with all the numbers entered on one line. However if you do not call nextInt inside the loop you will not get the next number.
ive found a sample for calling nextInt() in the loop, but it's array , and i dont want an array.

Java Code:
```for (int i = 0; i < input ; i++ )

{

numbers[i] = input.nextInt();```
any idea how can i do this without using array?

12. ## Re: Odd and Even Numbers

You don't need an array. Just move the assignment to input inside the loop. Use the hasNext() method to detect the end of input data.

13. Member
Join Date
Apr 2009
Posts
84
Rep Power
0

## Re: Odd and Even Numbers

Originally Posted by Norm
You don't need an array. Just move the assignment to input inside the loop. Use the hasNext() method to detect the end of input data.
is this what u meant? gives me the error incompatible types.

Java Code:
```for(int i=0;i<100;i++){
int input=sc.hasNext();
if (i%input==0){
evenCount++;

}```

14. ## Re: Odd and Even Numbers

You need to read the API doc for the Scanner class for the nextInt() and for the hasNext() methods.
They return different values and are used for different purposes.

I said: Use the hasNext() method to detect the end of input data.
Not to read values from the user.

15. Member
Join Date
Apr 2009
Posts
84
Rep Power
0

## Re: Odd and Even Numbers

Originally Posted by Norm
I said: Use the hasNext() method to detect the end of input data.
Not to read values from the user.
how am i suppose to detect the end of input data? any example?

16. ## Re: Odd and Even Numbers

Did you look at the API doc for the hasNext() or hasNextInt() methods? The return a boolean that can be tested. Might be a problem with blocking to wait for input.

pseudo code
begin loop
is there more input - if not exit the loop
process the input
end loop

You might want to use a "sentinel" value the user types in to tell you that there is no more input.

Or read in the whole line of numbers with nextLine() and work through them either using another instance of Scanner.
Here's a sample:
Java Code:
```      Scanner scnr = new Scanner("1 2  3 4 5\n 6 8 9 0");
while(scnr.hasNextInt()) {
System.out.print(scnr.nextInt() + " ");  // 1 2 3 4 5 6 8 9 0
}```
Last edited by Norm; 04-14-2012 at 01:34 PM.

17. Member
Join Date
Apr 2009
Posts
84
Rep Power
0

## Re: Odd and Even Numbers

i figured out the problem with my program,
Java Code:
`int input=sc.nextInt();`
in a for loop so it'll keep count of numbers entered by user ,
e.g:
Java Code:
```for (int i=0;i<10;i++){ // the loop keeps count of entered numbers,
int input=sc.nextInt();// here after each time user enters a numbers , loop count 1 , until 10 numbers are entered.
}```
the rest is up to if statement that calculates how many odd n even numbers are there. here's the final program:
Java Code:
```import java.util.Scanner;

public class oddeven {
public static void main (String [] args){
int evenCount=0;int oddCount=0;
Scanner sc=new Scanner(System.in);
System.out.print("enter 10 numbers : ");

for(int i=0;i<10;i++){
int input=sc.nextInt();

if (input%2==0){
evenCount++;

}
else
oddCount++;

}

System.out.print("u have entered "+evenCount+" even numbers and "+oddCount+" odd numbers");

}

}```