By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,557 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,557 IT Pros & Developers. It's quick & easy.

Really need help

P: 23
I need to know when a user inputs two integers how to output just the odd numbers?
Dec 8 '06 #1
Share this Question
Share on Google+
1 Reply


P: 26
I need to know when a user inputs two integers how to output just the odd numbers?
In java, there is a modulo operator, so you can determine whether an int is an odd or an even number . for example:

Expand|Select|Wrap|Line Numbers
  1. public void checkNumber(int x) {
  2.  
  3. if (x % 2 == 0) {
  4.   // then we have an even number
  5. System.out.println(x + " is an even number")
  6. }
  7.  
  8. if (x % 2 != 0) {
  9.   // then we have an odd number
  10. System.out.println(x + " is an odd number")
  11. }
  12.  
  13. }
  14.  
  15.  
If you are looking for ways to read those ints in for a mini-program, then you could look at a Scanner object at

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html
Dec 8 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.