Click to See Complete Forum and Search --> : Reading odd and even integers


October 1st, 1999, 11:12 AM
I'm trying to write a program in for school that will read whether a number is odd or even, and I just can't get anything to work.
I'm guessing I need if statements but I can't figure out exactly what, this is what I have so far...

import javax.swing.JOptionPane;

public class Even {
public static void main (String args[])
{
String num1;
int n1,odd,even;

num1 = JOptionPane.showInputDialog("Please type in an integer:");

n1 = Integer.parseInt(num1);
odd = ? ;
even = ? ;


if( ? )JOptionPane.showMessageDialog(
null,"The number is "+odd,"Results",JOptionPane.INFORMATION_MESSAGE);
else JOptionPane.showMessageDialog(
null,"The number is "+even,"Results",JOptionPane.INFORMATION_MESSAGE);

System.exit (0);
}
}

The ?'s are where I', completely lost, someone Please help!!!

scooby
October 1st, 1999, 12:15 PM
OK, get rid of even and odd completly. in your if statement, divide n1 by 2. if the remainder is 0 n1 is even. if the remainder is anything other than 0, it is odd. use the remainder operator (%) to do the division.

like this

if((n1%2)==0)
n1 is even
else
n1 is odd




hope this helped, and disregard my other reply