# using percent mod to count number of even number of user input

Page 1 of 1

## 13 Replies - 2441 Views - Last Post: 10 September 2011 - 01:51 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=246672&amp;s=f50316a12fdcfb79c2c5f66eab8a4790&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 kikiskia

Reputation: 0
• Posts: 28
• Joined: 10-September 11

# using percent mod to count number of even number of user input

Posted 10 September 2011 - 11:58 AM

ok so I need to use the percent mod operator or the remainder operator to calculate the number of integers total, and the number of even numbers..All this will be done by a set of numbers that the user will input.... so far I got this
```

package cis2168lab1p1;

import java.util.Scanner;

public class CIS2168Lab1P1 {

public static void main(String[] args) {

Scanner keyboard = new Scanner(System.in);

int sum;

int userInput= keyboard.nextInt();

sum=0;

int numberOfTotal=userInput;

while (userInput >=0)

{
sum=sum + userInput;
numberOfTotal++;
userInput= keyboard.nextInt();

if (userInput%2, count<=0, count++)

}

}

}

```

This is my first time programming and I really do not know that much. I need people to be specific like show me examples...Thanks...

Is This A Good Question/Topic? 0

## Replies To: using percent mod to count number of even number of user input

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 12:02 PM

This if has an invalid syntax

if (userInput%2, count<=0, count++)

what are you trying to do ?

if(userInpout % 2 == 0) then something ?

### #3 kikiskia

Reputation: 0
• Posts: 28
• Joined: 10-September 11

## Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 12:39 PM

this is my new code..I can not figure out how to do the total number...meaning calculating how many numbers the user inputs..also something is wring with calculting my even total...
```
package cis2168lab1p1;

import java.util.Scanner;

public class CIS2168LP1
{

public static void main(String[] args)
{
int userInputNumber;
int userInput = 0;
int evenNumbers = 0;

Scanner keyboard = new Scanner(System.in);

int [] numbers = new int [userInput];

System.out.println("Enter the numbers you want to count:   ");
userInput = keyboard.nextInt();

for (int i = 0; i < userInput; i++ )

{
int random = keyboard.nextInt();
if (random % 2 == 0)

{
evenNumbers++;
userInputNumber++;
}
}

System.out.println("Total even numbers " + evenNumbers);
System.out.println("Total Numbers used" +  userInputNumber);

}
}

```

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 12:50 PM

Should be

```     userInput = keyboard.nextInt();
int [] numbers = new int [userInput];

```

You have to read userInput before creating the array based on its value

And total numbers used is simply userInput

### #5 kikiskia

Reputation: 0
• Posts: 28
• Joined: 10-September 11

## Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 12:54 PM

ok this is the new one sorry about the other post......ummmm it is not counting the correct number of the user input...i need the total of the even and the unused ones...

```

package cis2168lab1p1;

import java.util.Scanner;

public class CIS2168LP1
{

public static void main(String[] args)
{

int userInput = 0;
int evenNumbers = 0;

Scanner keyboard = new Scanner(System.in);

System.out.println("Enter the numbers you want to count:   ");

int [] numbers = new int [userInput];
userInput = keyboard.nextInt();

for (int i = 0; i < userInput; i++ )

{
int random = keyboard.nextInt();
if (random % 2 == 0)

{
evenNumbers++;

}
}

System.out.println("Total even numbers " + evenNumbers);
System.out.println("Total Numbers used" +  userInput);

}
}

```

This post has been edited by kikiskia: 10 September 2011 - 12:57 PM

### #6 GregBrannon

• D.I.C Lover

Reputation: 2215
• Posts: 5,240
• Joined: 10-September 10

## Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 01:03 PM

Where is your counter for the number of entries made by the user? I see one counter for evenNumbers, and another variable for the value entered by the user. Where's the other counter?

### #7 kikiskia

Reputation: 0
• Posts: 28
• Joined: 10-September 11

## Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 01:07 PM

GregBrannon, on 10 September 2011 - 01:03 PM, said:

Where is your counter for the number of entries made by the user? I see one counter for evenNumbers, and another variable for the value entered by the user. Where's the other counter?

wouldnt it be the userInput??? or do I have to create another counter...Should I put it after the for loop???? ugh this is soo hard..

### #8 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 01:18 PM

Actually you do not even need the array you created

```	public static void main(String[] args)
{
int evenNumbers = 0;

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the numbers you want to count:   ");

int userInput = keyboard.nextInt();

for (int i = 0; i < userInput; i++ )
{
if(keyboard.nextInt() %2 == 0)
{
evenNumbers++;
}
}

System.out.println("Total Numbers used" +  userInput);
System.out.println("Total even numbers " + evenNumbers);
System.out.println("Total odd numbers  " + (userInput - evenNumbers));
}

```

### #9 kikiskia

Reputation: 0
• Posts: 28
• Joined: 10-September 11

## Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 01:30 PM

ok so I did your suggestion but the code wont run..like it says running and nothing appears to be happening after I enter the random integers....what should I do??? also I decided to add the input with the even integers to get me the total..because the question wasnt asked how many odd..I dont want any points to be deducted,,haha

```
package cis2168lab1p1;

import java.util.Scanner;

public class CIS2168LP1
{

public static void main(String[] args)	{
int evenNumbers = 0;

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the numbers you want to count:   ");

int userInput = keyboard.nextInt();

for (int i = 0; i < userInput; i++ )
{
if(keyboard.nextInt() %2 == 0)
{
evenNumbers++;
}
}

System.out.println("Total even numbers " + evenNumbers);
System.out.println("Total odd numbers  " + (userInput + evenNumbers));
}}

```

### #10 Fuzzyness

• Comp Sci Student

Reputation: 669
• Posts: 2,438
• Joined: 06-March 09

## Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 01:34 PM

```Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the numbers you want to count:   ");
int userInput = keyboard.nextInt();
for (int i = 0; i < userInput; i++ )
{
if(keyboard.nextInt() %2 == 0)
{
evenNumbers++;
}
}
```

When you do this, you only prompt for one count, the initial count. Inside of the for loop before the IF statement, you need to add another prompt asking for another number.

### #11 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 01:39 PM

Fuzzyness, on 10 September 2011 - 04:34 PM, said:

```Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the numbers you want to count:   ");
int userInput = keyboard.nextInt();
for (int i = 0; i < userInput; i++ )
{
if(keyboard.nextInt() %2 == 0)
{
evenNumbers++;
}
}
```

When you do this, you only prompt for one count, the initial count. Inside of the for loop before the IF statement, you need to add another prompt asking for another number.

Not really... this code works pefectly for me
Actually should be: "Enter the number of numbers you want to count"

### #12 kikiskia

Reputation: 0
• Posts: 28
• Joined: 10-September 11

## Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 01:44 PM

ok should I do something like
```
package cis2168lab1p1;

import java.util.Scanner;

public class CIS2168LP1
{

public static void main(String[] args)	{
int evenNumbers = 0;
int userInput=0;
Scanner keyboard = new Scanner(System.in);

userInput = keyboard.nextInt();

for (int i = 0; i < userInput; i++ )
{
if(keyboard.nextInt() %2 == 0)
{
evenNumbers++;
userInput++;
}
}

//System.out.println("Total even numbers " + evenNumbers);
System.out.println("Total odd numbers  " + (userInput + evenNumbers));
}}

```

### #13 GregBrannon

• D.I.C Lover

Reputation: 2215
• Posts: 5,240
• Joined: 10-September 10

## Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 01:45 PM

pbl's version didn't work for me, but changing the prompt helps. Here's another approach, very close to what you had but with minor mods:

add a counter for the number of numbers entered,
instruct the user to enter '0' to end,
use a while loop that terminates the loop with '0',
count total numbers entered in the while loop,
get userInput at end of while loop, and
add a space before printing totalNumbers.

```import java.util.Scanner;

public class EvenNumberCounter
{
public static void main(String[] args)
{
int evenNumbers = 0;
int totalNumbers = 0;
int userInput = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the numbers you want to count (0 to end):");

userInput = keyboard.nextInt();

while ( userInput > 0 )
{
totalNumbers++;
if( userInput %2 == 0)
{
evenNumbers++;
}
userInput = keyboard.nextInt();
}
System.out.println("Total Numbers used " +  totalNumbers );
System.out.println("Total even numbers " + evenNumbers );
System.out.println("Total odd numbers  " + ( totalNumbers - evenNumbers ) );
}
}
```

### #14 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: using percent mod to count number of even number of user input

Posted 10 September 2011 - 01:51 PM

Use the exact code that is in Fuzzyness reply #10
You can enter each number on different lines or multiple numbers per line as you wish

5 123 456 22 34 12

or

5
12
123
456
567
2

or

5
12 34
356 786
23