Ok so here is what I have and I am stuck. It is not recognizing whether a number is even or odd. So where do I need to put the remainder operator so that it will recognize if a number is even or odd?

import javax.swing.JOptionPane;  // program uses JOptionPane
public class EvenOdd {


/** Creates a new instance of EvenOdd */
public EvenOdd() {
}


/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String Number;        // string entered by user
String result;        // a string containing the output


int number;           // number to be read
int even;            // number if even
int odd;             // number if odd


// read number from user as a string
Number = JOptionPane.showInputDialog( null, " Enter a number" );


// convert number from type String to type int
number = Integer.parseInt( Number );


// initialize result to empty String
result = "";


if ( number%2 == 0/2 );
result = result + Number + " is EVEN ";


// Display results
JOptionPane.showMessageDialog( null, result, " Results ", JOptionPane.INFORMATION_MESSAGE );


System.exit ( 0 );   // terminate application


if ( number%2 != 0/2 );
result = result + Number + " is ODD ";


// Display results
JOptionPane.showMessageDialog( null, result, " Results ", JOptionPane.INFORMATION_MESSAGE );


System.exit ( 0 );   // terminate application



} // end method main


}  // end class EvenOdd

Recommended Answers

All 5 Replies

I think I've got it. However, I'm very new to Java Programming, so it may not be complete, and you may have questions about it. When you wrote:

if ( number%2 == 0/2 );

nothing else that you wrote after that applied to that if statement. By replacing that with:
"if ( number%2 == 0/2){" and ending the curly braces before your next if statement, I think the code will work properly. I briefly tested the code, and for my intents and purposes, it works. However, I'd check it over and look at it.

import javax.swing.JOptionPane; // program uses JOptionPane
public class EvenOdd {

/** Creates a new instance of EvenOdd */
public EvenOdd() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String Number; // string entered by user
String result; // a string containing the output

int number; // number to be read
int even; // number if even 
int odd; // number if odd

// read number from user as a string
Number = JOptionPane.showInputDialog( null, " Enter a number" );

// convert number from type String to type int
number = Integer.parseInt( Number );

// initialize result to empty String
result = "";

if ( number%2 == 0/2 );
result = result + Number + " is EVEN ";

// Display results
JOptionPane.showMessageDialog( null, result, " Results ", JOptionPane.INFORMATION_MESSAGE );

System.exit ( 0 ); // terminate application

if ( number%2 != 0/2 ){
result = result + Number + " is ODD ";
}
// Display results
JOptionPane.showMessageDialog( null, result, " Results ", JOptionPane.INFORMATION_MESSAGE );

System.exit ( 0 ); // terminate application


} // end method main

} // end class EvenOdd

You should havesomething like:
if ( number % 2 == 0 ){
// The number is EVEN
} else
// the number is ODD
}
number % 2 - will result in 0 if the number is divisable (has no remainder) by 2

Modulas operations return any remainder of the number (in your example 2).

So if you wanted to know if a number is divisable by 10, you could write
number % 10 and it will return a 0 for numbers like 10, 20, 100, 300, etc

tnx it help!

I'm pretty sure the OP (and other posters) in this thread already noticed that, some 7 years ago, when the last post here was entered. saying "hey, it helps!!!" is no reason to revive an ancient thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.