13

`I'm not sure what code to insert or even where, but I would like to check the number I enter is an odd number.

import java.io.*;
import javax.swing.JOptionPane;

public class Diamond {
    public static void main(String [] args) throws IOException {

        BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
        String input; 
        int num;
        System.out.println("input number: ");
        input = stdin.readLine ();
        num = Integer.parseInt(input);

        if (num % 2 ==1){
            int d = num;
            int e = 0;
            for (int a = 0; a <= num; a++) {
                for (int c = d; c>= 1; c-- )
                    System.out.print(" ");
                d-=1;
                for (int b = 1; b <= a; b++)
                    System.out.print ("* ");
                System.out.println();
            }

            num-=1;
            for (int a = 0; a<=num; a++) {
                for (int b = num; b > a; b--)
                    System.out.print (" *"); 
                System.out.println(); 
                for (int c = 0; c <= e; c++)
                    System.out.print(" ");
                 e+=1;
            }
        } else {
            System.out.println("Please enter an odd number!");
        }
    }
}
Joel
  • 7,401
  • 4
  • 52
  • 58
Mike
  • 2,293
  • 13
  • 42
  • 56

5 Answers5

52

Use modular arithmetic:

if (number % 2 == 0) {
  // even
} else {
  // odd
}

Update:

You can test this code here:

Beware that checking for evenness with number % 2 == 1 will fail.

To check if a number is odd, you can use (number & 1) != 0.

miku
  • 181,842
  • 47
  • 306
  • 310
  • Just as a quick reply, in Python you could do: `int(z / 2) == float(z) / 2` but most will use the obvious `%`. – miku Dec 23 '11 at 11:26
  • 1
    `number % 2` means the remainder of dividing number by 2. All even numbers will not produce a remainder, and all odd numbers will. – Mark W Jun 25 '15 at 12:03
  • Incorrect results on negative numbers… down vote. :( – geowar May 21 '17 at 01:04
11

num % 2 == 1 return incorrect results on negative Odd number, the remainder of division with 2 will not be 1. This can be fixed as follows:

public boolean isOddNumber(int num) {
    return (num & 1) != 0;
}
Matt
  • 3,676
  • 3
  • 34
  • 38
Chaker
  • 1
  • 1
  • 2
6

You can NOT have readLine inside of that if. First you need to get the value and next you can use your if.

It goes like this:

BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));

String input; 
int num;

System.out.println("input number: ");

  input = stdin.readLine();
  num = Integer.parseInt(input);

if (num % 2 == 1) {
// odd
} else {
  System.out.println("Please enter an odd number!");
}

Finally - do NOT use values named "a", "e" or "d" - it's very confusing. Just name vars with names that let reader know/guess their role in your code. I have no idea what is the meaning of your "a" or b, c, d etc. For example, your num should be named enteredValue to clarify your code.

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
dantuch
  • 9,123
  • 6
  • 45
  • 68
  • I changed it, but something is not where it should be. When I enter an even number it does nothing. If I enter an odd number it works fine. – Mike Mar 11 '11 at 20:51
  • Id like to help u, but ur code is awful to read. U need to rename ur variables... add some private functions in place of multiple code line inside of if-else -> if - function1() (named correctly!) else - function2() ... http://introcs.cs.princeton.edu/11style/ read it plz, or google sth else bout **clear code** and remeber - code needs to be easy to read! – dantuch Mar 11 '11 at 21:28
  • 2
    Please refrain from using text-speak in your answers and your comments. This is a site for *professional and enthusiast programmers*, and as such expects at least some level of professionalism in questions and answers. – Rob Hruska Mar 11 '11 at 21:29
  • @Rob Hruska so be profesional and help Mike in place of learning me to use "you" in place of "u". I just want to help, not to act like someone else – dantuch Mar 11 '11 at 21:35
  • 4
    Using full words (e.g. "you" instead of "u", "your" instead of "ur") makes your answers easier to read and comprehend. Since there's no limit on the size of the content, there's no reason not to take the time to spell things out and make your answers legible. It will also help others to take your answer more seriously, so it serves your interests as someone trying to help. – Rob Hruska Mar 11 '11 at 21:40
2

Chaker's answer about negative integer is confirmed. In my JavaSE-1.8

System.out.println( "result =" + ( -3 % 2 == 1) );


it shows result =false instead true

Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
jay fall
  • 184
  • 1
  • 4
1

Bitwise operation (bit manipulation) way in Java

if ((num & 1) != 0) //odd
{
     //do something here
} else { //even
    //do something here
}

works by looking at say, 100 (number 4) vs 001, doing the AND operation on each bits and returning either 0 and 1. If the end bit is a 0, like 4, it's always going to be even. If the end bit is 1, it is going to be odd.

John61590
  • 1,106
  • 1
  • 13
  • 29