0

I'm trying to make a java program that takes a number given by the user, and displays whether it is odd or even. I am having trouble, i think it might be tht my logic is wrong idk. It just prints the never ending message "odd"

import java.util.Scanner;

public class Questions {
  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.println("Your number");
    int number = input.nextInt();
    for (int i = 0; i <= number; i = +2)
      if (number == i) {
        System.out.println("even");
      } else {
        System.out.println("odd");
      }
  }
}
durron597
  • 31,426
  • 16
  • 97
  • 154
John Jay
  • 7
  • 3

5 Answers5

1

You have an endless for statement, because you don't change a loop variable i. You might try the following pieces of code to decide your task:

System.out.println(number % 2 == 0 ? "even" : "odd");

or

System.out.println((number & 1) == 0 ? "even" : "odd");

Those lines will give same result:

(number = 2)
even

Of course, in your case, you might change this line for (int i = 0; i <= number; i = +2) to the next line for (int i = 0; i <= number; i += 2), but an output won't give an expected result:

(number = 2)
odd
even
Andrew Tobilko
  • 46,063
  • 13
  • 87
  • 137
1

In your for loop you have the assignement

i =+ 2

This sets the variable i to positive 2. To add 2 to the variable use:

i += 2

This would print "odd" as long as i is smaller than the input value and print a final "even" if the value is event or "odd" if the value is odd.

Btw to test if a value is a multiple of another value you can use

if ((value % otherValue) == 0) {}

so in your example

if ((number % 2) == 0) {
    System.out.println("even");
} else {
    System.out.println("odd");
}

or even shorter

System.out.println((number % 2) == 0 ? "even" : "odd");
Tiim
  • 181
  • 1
  • 12
1

Your check for an odd / even number is wrong and could be optimised. Use the modulus operator instead:

if (number % 2 == 0) {
    System.out.println("Even");
} else {
    System.out.println("Odd");
}

The modulus gives you the remainder of the division of the operands. If you divide by 2 and the remainder is 0 - you have an even number.

Danail Alexiev
  • 7,234
  • 3
  • 18
  • 27
1

you are make mistake in the for statement

for (int i = 0; i <= number; i = +2)// this will not increment the counter, it will set it every time with  2 positive

for (int i = 0; i <= number; i +=2)// this will increment the counter by 2 in every iteration 

also you should try to find the problem by debugging or use some print statements after loop or in condition to troubleshot this issue.

Ahmad Al-Kurdi
  • 2,092
  • 2
  • 19
  • 36
1

This program never terminates.

for(int i = 0; i <= number; i=+2)

The offending code is that last item. Incrementing i by 2 looks like this:

i += 2;

If you write it like this:

i =+ 2;

Then it will just repeatedly assign the value '2' to i.

Of course, as the other posters already pointed out, this isn't how you should test evenness/oddness. You should either perform a modulo check:

if(i % 2 == ) <even>
else <odd>

Or simply perform a bitwise comparison on the last digit.

if(i & 1 == 0) <even>
else <odd>
Xirema
  • 19,266
  • 4
  • 30
  • 64