0

I have the following homework problem:

Write an application integer on that reads an integer and determines and prints whether it is odd or even.

4
  • Is this homework? If so, what have you tried so far? Commented Apr 3, 2011 at 13:37
  • should be tagged as homework. i%2==0 Commented Apr 3, 2011 at 13:38
  • It's a homework, isn't it? And maybe you should show what you've done so far. Commented Apr 3, 2011 at 13:39
  • This sounds like a homework question, and probably should be tagged as such. As a rule, the community gives hints on homework questions only when you've shown sufficient effort to attempt to solve the problem first. Commented Apr 3, 2011 at 13:39

2 Answers 2

12

Use the Scanner class for reading input.

Store that input into an integer. Check if input is really a valid integer. Otherwise, throw an exception.

Afterwards, use the modulo operator to check if it's odd or even.

Use System.out.println to print if it's odd or even.

Sign up to request clarification or add additional context in comments.

2 Comments

@The Elite Gentleman: I didn't really solve it; I just gave a description of what to do. OP must gather the pieces to solve the puzzle. Writing the actual code would be solving his homework.
+1. @Elite I think @darioo answered exactly the way one should, concerning homework questions.
0
public static void main(String[] args) {

    System.out.println("Enter number");

    Scanner sc = new Scanner(System.in);

    try {

        int i = sc.nextInt();
        if (Math.abs(i) / i == 1) {
            if ((Math.abs(i) % 2) == 0) {
                System.out.println("Even");
            } else {
                System.out.println("Odd");
            }
        }

    } catch (Exception ex) {
        System.out.println("Exception " + ex.toString());
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.