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.
Solution
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.
Similar Questions
code this in java using patterns: Specification: Read in integers until the user enters -1. If there was at least one integer, show the average value.
Write the program to enter integer elements from user in singly linked list. User can enter both andeven values.
Read a number from the user. Check whether it is odd or even using lambda function and print the output as either ODD or EVEN. Sample Input5Sample OutputODD
Write an HTML and JavaScript program which accepts N as input and print first Nodd numbers
18. int value = 453;19. int d1 = value % 10;20. int d2 = (value / 10) % 10;21. int d3 = (value / 100) % 10;22. System.out.println("" + d1 + d2 + d3);23. }24. }
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.