Wednesday, July 1, 2015

// // Leave a Comment

Program To Check If A Number Is Odd Or Even

The program below is the answer to Deitel's Java How to Program (9th Edition) Chapter 2 Exercise 2.25.

Question: Write an application that reads an integer and determines and prints whether it’s odd or even. [Hint: Use the remainder operator. An even number is a multiple of 2. Any multiple of 2 leaves a remainder of 0 when divided by 2.]

/**
 *
 * @Author: Aghatise Osazuwa
 * Website: www.cscprogrammingtutorials.com
 *
 * Exercise 2.25 - Odd Or Even
 * This Program Determines If A Number Is Odd Or Even
 *
 */ 

import java.util.Scanner;

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

 Scanner numbers = new Scanner (System.in);        

 int num;
        int check;
        
        System.out.print ("Enter A Number: ");
        num = numbers.nextInt();
        check = num%2;
        
        if (check == 0)
            System.out.printf ("\nThe Number %d is EVEN.\n", num);
        if (check != 0)
            System.out.printf ("\nThe Number %d is ODD.\n", num);

    }
}

0 comments:

Post a Comment


DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately.

Thanks.