Results 1 to 5 of 5

Thread: Java : test a number to find out if its is even or odd

  1. #1
    Join Date
    Jul 2009
    Posts
    1,179

    Java : test a number to find out if its is even or odd

    Hi,

    I am learning Java. I want to do a program that can check a number if its odd or even. I have done it with C but now need your help with Java coding.

    Thanks for the help in advance.

    teenQ

  2. #2
    Join Date
    May 2008
    Posts
    31

    Re: Java : test a number to find out if its is even or odd

    Logic:

    Code:
    if (x % 2 == 0) {
      // even 
    }
    
    if (x % 2 != 0) {
      // odd
    }
    
    ... or binary AND operator...
    
    if (( x & 1 ) == 0) {
      // even
    }
    
    if (( x & 1 ) != 0) {
      // odd
    }
    I hope this helps you.

  3. #3
    Join Date
    May 2008
    Posts
    22

    Re: Java : test a number to find out if its is even or odd

    Check Even-Odd:

    Code:
     import java.io.*;
    
    public class IfElse{
      public static void main(String[] args) throws IOException{
        try{
          int n;
          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
          n = Integer.parseInt(in.readLine());
          if (n % 2 == 0)
          {
            System.out.println("Given number is Even.");
          }
          else
          {
            System.out.println("Given number is Odd.");
          }
        }
        catch(NumberFormatException e){
          System.out.println(e.getMessage() + " is not a numeric value.");
          System.exit(0);
        }
      }
    }
    This is your Java code.

  4. #4
    Join Date
    Jul 2009
    Posts
    1,179

    Re: Java : test a number to find out if its is even or odd

    Hey Thanks for such a prompt reply.

    I am running the code right now.

    Thanks.

  5. #5
    Join Date
    Apr 2012
    Posts
    1

    Re: Java : test a number to find out if its is even or odd

    I am in AP Java (high school). I'll show you how I'd check for even or odd!
    I do not know how to make that box-within-a-reply, so I'll just enter the program application here:

    Code:
    import java.util.Scanner;
    public class EvenOrOdd {
         public static void main(String[] args) {
              Scanner input = new Scanner(System.in);
              int num = 0; 
    
              System.out.println("Enter a number: ");
              num = input.nextInt();
                   
              if (num % 2 == 0) {
                   System.out.println("The number you entered is even");
              } else if (num % 2 != 0) {
                   System.out.println("The number you entered is odd");
              }
         }
    }

Similar Threads

  1. Java speed test giving error
    By Eduards in forum India BroadBand
    Replies: 5
    Last Post: 05-04-2011, 10:23 AM
  2. Publish tool for Java JMS load test
    By Sheravat in forum Software Development
    Replies: 9
    Last Post: 18-09-2010, 10:00 PM
  3. How to write java program to find factorial of number?
    By Balamohan in forum Software Development
    Replies: 5
    Last Post: 28-11-2009, 10:14 PM
  4. how to find median of given number in java?
    By Aloke in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 09:27 PM
  5. Java String test
    By Aidan 12 in forum Software Development
    Replies: 3
    Last Post: 09-11-2009, 02:22 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,481,336.43931 seconds with 17 queries