Create a Java program that uses the conditional operator to assign a value to a boolean variable. You should assign true if both numbers entered by the user (x, y) are even, and false if either of them is odd. Finally, display the value of the variable on the screen.
boolean
true
false
2 2
import java.util.Scanner; public class ConditionalAssignment { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Prompt the user for two integers System.out.print("Enter the first number (x): "); int x = scanner.nextInt(); System.out.print("Enter the second number (y): "); int y = scanner.nextInt(); // Use the conditional operator to assign a value to the boolean variable boolean bothAreEven = (x % 2 == 0 && y % 2 == 0); // Display the value of the variable on the screen System.out.println("Are both numbers even? " + bothAreEven); scanner.close(); } }
Click here to view the exercise solution
Share it on your social media and challenge your friends to solve programming problems. Together, we can learn and grow.
The code has been successfully copied to the clipboard.
Continue improving your Java programming skills with our selection of practical exercises from the lesson. Click on Practice and challenge your knowledge!
Java basic calculator. Input numbers and operators (+, -, x, /) for calculation. If the symbol is not valid, it displays 'Character not recognized'.
Calculate and display the absolute value of a number in Java with this programming exercise on conditions.
Basic calculator with switch for mathematical operations. Allows decimal numbers and error handling. Practice your skills in Java.
Learn Java: Detect whether a number is positive or negative in this programming exercise. Enhance your Java skills.
Create a Java program that prompts the user for a letter (x) and checks whether it is a vowel or any other symbol.
Create a Java program that uses the conditional operator to assign value to a boolean variable.
Java exercise to find the largest number among three and practice Java conditions.
Create a Java program that prompts the user to enter two integers (a, b) and determines how many of them are positive using nested conditional operators in Java.
Calculate a student's grade using a switch statement in Java based on an integer input. Practice using switch, break, and default.
Practice with exercises in Java related to the conditions, loops and structures of the language.
Free programming course with practical exercises and solutions in C#. Start learning right now!
Take your Exercises C# lessons everywhere with our Android app. Download it now from Google Play
Own and third party cookies to improve our services. If you go on surfing, we will consider you accepting its use.