0

Trying to create a program that asks the user to input a number between 20 and 100. After the number has been entered. The program will divide the entered number by 12. The program will then say if the result of the division is even or odd. (example: the remainder of 35 divided by 12 is 11, and it is odd.)

I have started the program but cannot figure out the division part.

import java.util.Scanner;

public class Chpt3_Project {
    public static void main (String [] args) {
        // Create a Scanner object
        Scanner sc = new Scanner(System.in);

        // Prompt the user to enter an integer value between 20 and 100.
        int input;
        do {
            System.out.print("Enter a number between 20 and 100: ");
            input = sc.nextInt();

            if (input < 20 || input >= 101) {
                System.out.println("Invalid number input!");
            }
        } while (input < 20 || input >= 101);

        //Divide result by 12 and show if even or odd

    }

}
Torben
  • 3,805
  • 26
  • 31
  • If any of the answers was helpful to you, you could [accept](http://i.stack.imgur.com/QpogP.png) the answer you found most helpful. – Thomas Flinkow Sep 14 '18 at 07:17

2 Answers2

2

You can use the modulo operator to check whether a number is even or odd. Let's say you have

int n = 7;

applying the modulo operator

int r = n % 2;

will yield 1 - as what happens is this:

Divide n by 2 and return the remainder.

So, we know that if the remainder of a % 2 operation is 0, the number is even, otherwise, if the remainder is 1, the number is odd.


In your case, the code could look like this:

public static void main (String [] args) {
    // Create a Scanner object
    Scanner sc = new Scanner(System.in);

    // Prompt the user to enter an integer value between 20 and 100.
    int input = 0;

    do {
        System.out.print("Enter a number between 20 and 100: ");
        input = sc.nextInt();

        if (input < 20 || input >= 101) {
            System.out.println("Invalid number input!");
        }
    } while (input < 20 || input >= 101);

    // Divide the input by 12 and check if the remainder is an even number (== 0).
    boolean isEven = (input % 12) % 2 == 0;

    if(isEven) {
        System.out.println("Input is an even number.");
    }
    else {
        System.out.println("Input is an odd number.");
    }
}
Thomas Flinkow
  • 4,845
  • 5
  • 29
  • 65
  • 1
    Don't forget the division by 12 before the odd even check. – Adder Sep 13 '18 at 07:56
  • @Adder thank you for that, edited it right now. – Thomas Flinkow Sep 13 '18 at 07:57
  • 1
    `remainder == 0` checks for an even number, `remainder == 1` checks for an odd number. It is correct in the explanation but not in the code. – Adder Sep 13 '18 at 08:00
  • @Adder thanks again. English is not my first language; I sometimes confuse "odd" and "even". It should be correct in the code now, isn't it? – Thomas Flinkow Sep 13 '18 at 08:01
  • @ThomasFlinkow Thank you for the fast response. After the integer is placed in the program it shows if it's even or odd which is great but I would like it to if it is odd or even only after it is divided by 12. This is what I'm trying to achieve in the console: Enter a number between 20 and 100: 35 The remainder of 35 divided by 12 is 11, and it is odd – Marco Del Monte Sep 13 '18 at 08:09
  • @MarcoDelMonte please check my answer again. I modified it to do exactly what you described – Thomas Flinkow Sep 13 '18 at 08:20
0

In order to do the math you have to first retrieve the result of modular division of N mod(12) and then check if the remainder is dividable by 2 -> ((N mod(12) mod(2)).

int remainderAfterDivisionByTwelve = n % 12; // n = 35 -> results in 11
boolean isRemainderEven = (remainder % 2) == 0; // remainder = 11 -> results in (1 == 0) false
dbl
  • 1,109
  • 9
  • 17