Knowee
Questions
Features
Study Tools

Create a program in Java that will accept two integers. If the sum of the integers is odd, display the higher integer, else, display the lower integer.

Question

Create a program in Java that will accept two integers. If the sum of the integers is odd, display the higher integer, else, display the lower integer.

🧐 Not the exact question you are looking for?Go ask a question

Solution 1

Sure, here is a simple Java program that fulfills your requirements:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the first integer:");
        int num1 = scanner.nextInt();

        System.out.println("Enter the second integer:");
        int num2 = scanner.nextInt();

        int sum = num1 + num2;

        if (sum % 2 != 0) {
            System.out.println("The sum is odd. The higher integer is: " + Math.max(num1, num2));
        } else {
            System.out.println("The sum is even. The lower integer is: " + Math.min(num1, num2));
        }
    }
}

This program first asks the user to input two integers. It then calculates the sum of these integers. If the sum is odd, it displays the higher of the two integers. If the sum is even, it displays the lower of the two integers.

This problem has been solved

Solution 2

Sure, here is a simple Java program that fulfills your requirements:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the first integer:");
        int num1 = scanner.nextInt();

        System.out.println("Enter the second integer:");
        int num2 = scanner.nextInt();

        int sum = num1 + num2;

        if (sum % 2 != 0) {
            System.out.println("The sum is odd. The higher integer is: " + Math.max(num1, num2));
        } else {
            System.out.println("The sum is even. The lower integer is: " + Math.min(num1, num2));
        }
    }
}

This program does the following:

  1. It imports the Scanner class which is used to get user input.
  2. It defines a main method which is the entry point for any Java program.
  3. Inside the main method, it creates a Scanner object to read the user input.
  4. It prompts the user to enter the first and the second integer.
  5. It calculates the sum of the two integers.
  6. If the sum is odd (i.e., if the remainder of the division of the sum by 2 is not 0), it prints the higher integer using the Math.max method.
  7. If the sum is even (i.e., if the remainder of the division of the sum by 2 is 0), it prints the lower integer using the Math.min method.

This problem has been solved

Similar Questions

Write an algorithm, draw corresponding flow chart and write an interactive program which prompts the user with the following options on the opening menu: 1) To accept two integers and check whether they are equal or not 2) To check whether a given number is even or odd 3) To check whether a given number is a positive number or a negative number 4) Quit Enter your choice:Note: Use SWITCH..CASE statement.

Write a program to accept an integer N and print the sum of first N odd numbersInput Format:An integer range is given inputOutput Format:Print only calculated sum.Constraints:1 <= N <= 10^16 Sample Input 1:2Sample Output 1:4Sample Input 2:10Sample Output 2:100

(This is from Programming Exercise P4.1 at the end of Chapter 4 of Java for Everyone.) Write program fragments (i.e., you do not need to write complete programs) with loops that compute:The sum of all even numbers between 2 and 100 (inclusive).The sum of all squares between 1 and 100 (inclusive).All powers of 2 from 20 up to 220 (inclusive).The sum of all odd numbers between a and b (inclusive), where a and b are integer variables with a ≤ b.The sum of all digits at odd positions (right-to-left starting at 1 as the right-most digit) of a numeric input. (For example, if the input is 432677, the sum would be 7 + 6 + 3 = 16.)The sum of all digits at odd positions (left-to-right starting at 1 as the left-most digit) of a numeric input. (For example, if the input is 432677, the sum would be 4 + 2 + 7 = 13.)

answerWhat will be the output of the following Java program?class output_odd   {    public static void main(String args[])     {            int p = 2;       int q = 0;       for ( ; q < 8; ++q)        {         if (q % p == 0)            continue;           else if (q == 6)           break;         else          System.out.print(q + " ");       }    }   }

Develop a multithreaded Java program with two threads. One threadgenerates even numbers from 1 to 100 and another thread generates oddnumbers from 1 to 100. Ensure that the threads do not interfere.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.