# Thread: Check if int divides evenly

1. Senior Member
Join Date
Jan 2012
Posts
147
Rep Power
0

## Check if int divides evenly

How do I check if an integer divides evenly? I know I have to use the % symbol. The thing I thought about using was %>1 but I don't think you can do that.

2. ## Re: Check if int divides evenly

What have you tried with the % symbol? It's called the mod operator, by the way.

3. Senior Member
Join Date
Jan 2012
Posts
147
Rep Power
0

## Re: Check if int divides evenly

Java Code:
```public static void main (String[ ] args)
{

Scanner sc = new Scanner(System.in);

int firstNumber;
System.out.println("Input a number");
firstNumber = sc.nextInt();

int secondNumber;
System.out.println("Input another number");
secondNumber = sc.nextInt();

}  //======================
}```

that's what I have riht now

4. AN21XX
Join Date
Mar 2012
Location
Munich
Posts
297
Rep Power
5

## Re: Check if int divides evenly

The mod operator gives you the rest of a division... if it divides evenly you have how much left?
You may just check the result by using the mod operator like a division sign and see what the results are if you do not know how to proceed.

5. Senior Member
Join Date
Jan 2012
Posts
147
Rep Power
0

## Re: Check if int divides evenly

got it, thank you! I also have a math question.

If I had a game where I pick a number and tell you that the number you guessed is hot (2 or less to my number), warm (5 to 3 less to my number) how would I write that mathimatically?
yourNumber = 3;
myNumber = 5;
warmNumber = 5;
hotNumber = 2;
wNGuess =< myNumber + 5;
hNGuess =< mynumber + 2;

Would that work?

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

## Re: Check if int divides evenly

You would create the difference between the two numbers and use Math.abs()on the result to determine how far one number is from another. Zero is equal.

7. Member
Join Date
Dec 2012
Location
Des Moines, IA
Posts
35
Rep Power
0

## Re: Check if int divides evenly

You can tell classes are starting up again. I think I've seen this example or one very close to it a lot over the last week.

8. Senior Member
Join Date
Jan 2012
Posts
147
Rep Power
0

## Re: Check if int divides evenly

Exercise 4.27* Revise the GuessNumber game to say "you're hot" if within 2 of the right answer, "you're warm" if within 6 of the right answer, and "too high" or "too low" otherwise.

Java Code:
```import javax.swing.JOptionPane;
public class GuessNumber extends BasicGame {
private java.util.Random randy;
private int itsSecretNumber;
private int itsUsersNumber;
public GuessNumber(){
super();
randy = new java.util.Random();
} //=======================
itsSecretNumber = 1 + randy.nextInt (10);
} //=======================
String s = JOptionPane.showInputDialog
("Guess my number from 1 to 100:");
if (s != null && ! s.equals ("")) {
itsUsersNumber = Integer.parseInt (s);
}else{
itsUsersNumber = -1; // just to have a value there
}

} //======================
public boolean shouldContinue(){
return itsUsersNumber != itsSecretNumber;
} //======================
//Exercise 4.28
public void showUpdatedStatus(){
if (itsSecretNumber <= itsUsersNumber + 2 || itsSecretNumber >= itsUsersNumber - 2){
JOptionPane.showMessageDialog(null, "You're hot!");
}else
if (itsSecretNumber <= itsUsersNumber + 5 || itsSecretNumber >= itsUsersNumber - 5){
JOptionPane.showMessageDialog(null, "You're warm.");
}
if (itsUsersNumber > itsSecretNumber)
JOptionPane.showMessageDialog (null, "Too high");
else
JOptionPane.showMessageDialog (null, "Too low");
} //=======================
// inherited from BasicGame:
// playManyGames
// playOneGame
// showFinalStatus
}```
showUpdatedStatus() is what has been revised. Whenever I play the game and I guess it only says "You're hot!" and I'm not even 2 away! Once I was 11 away. I don't know what to do.

9. AN21XX
Join Date
Mar 2012
Location
Munich
Posts
297
Rep Power
5

## Re: Check if int divides evenly

10. Senior Member
Join Date
Jan 2012
Posts
147
Rep Power
0

## Re: Check if int divides evenly

I used the Math.abs() method. Works fine, thanks.
Last edited by Army; 01-08-2013 at 08:09 PM.

#### Posting Permissions

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