-4

I need to make it so the computer figures out whether an int somebody entered is even or not. How would I prove that? I tied just dividing it by two, but that isn't a boolean, and I don't know how I would figure it out with booleans either. Also would I need a separate counter to count the evens?

import java.util.Scanner; 
public class evenNumber {
 public static void main(String[] args) {
 Scanner scan = new Scanner(System.in);
 int numberOne = 0; 
  for(int counter = 0; counter < 15; counter++)
   System.out.print("Enter a number: ");
   numberOne = scan.nextInt(); 
  if(numberOne/2  ){

  }
 }
}
Bill
  • 29
  • 4
  • 8
    Try the modulus operator `%`. – Matt Jul 24 '13 at 03:08
  • What is the point of answering a question like this? Countless duplicates already exist. It should be downvoted and closed. – jahroy Jul 24 '13 at 03:13
  • I asked two questions jahroy, look and you will see the second one, it isn't a complete copy. – Bill Jul 24 '13 at 03:14
  • It is useful for me, I am trying to learn and I asked some people for help, I'm sorry I have nothing to offer when it comes to coding knowledge, also I didn't see the other post sorry. – Bill Jul 24 '13 at 03:17
  • 1
    I'm not saying this question wasn't useful to you. You have nothing to apologize for. I am speaking to the people who chose to answer this question rather than linking to one of the many duplicates that already exist for this question. – jahroy Jul 24 '13 at 03:18

5 Answers5

4

use the modulo operator :

if (varName % 2 == 0) {
    //divisible by two
} else {
    //not
}
krishnakid
  • 108
  • 5
  • But how would I count the evens, I already am counting how many times the person is entering a number. – Bill Jul 24 '13 at 03:11
  • 1
    To count the evens, make an integer `evenCount` and every time it returns even, simply say `evenCount++;` – Ryan Sparks Jul 24 '13 at 03:13
  • not sure I understand your question -- this if statement will determine if a number is even or not. – krishnakid Jul 24 '13 at 03:13
  • My teacher made it seem as if counter was a special type of code, I didn't know I could make variations, thanks. – Bill Jul 24 '13 at 03:15
  • `evenCount` could just be an iterating variable. Counting doesn't have to be a difficult task, just add one to the variable every time you want one to be added. – Ryan Sparks Jul 24 '13 at 03:17
1

You could do something like this:

if( number % 2 == 0){
 //it is even
}

% returns the reminder of a division and if there is no remainder(i.e it is zero) it was divisible that number. In this case if it is divisible by 2 it is even.

CBIII
  • 845
  • 1
  • 9
  • 9
0

This can be easily accomplished through the % operator. Try something like

if(numberOne % 2 == 0)
    //Number is even
} else {
    //Number is odd
}

Explanation:

The % operator gets the remainder of the value divided by the value. So numberOne % 2 would either return 0 if it can be divided by 2, or 1 if it can't.

If you wanted to count the evens, declare a variable int evenCount, and every time if(numberOne % 2 == 0), simply go evenCount++

Ryan Sparks
  • 309
  • 4
  • 17
0
if(number%2 == 0)
System.out.println("even");
else
System.out.println("odd");

The "%" gives you the remainder after division like 3%3 =0, 4%3=1, 5%3=2, 6%3=0

-2

I would use bitwise and:

if ((number & 1) == 0) {
    // Even.
} else {
    // Odd.
}

This is basically checking to see if the low order bit is zero.

This is much faster than using the modulus operator (%).

Rob
  • 6,247
  • 2
  • 25
  • 33