Olivia likes to play with numbers. She told her friends about a game in which Olivia would tell them three numbers and they would have to find the smallest among them. As it was time-consuming, her friends decided to write a program to get three numbers and print the smallest one. Help them by developing an application using your Java skills.Note:Use ternary operator.If all 3 numbers are same then print "All numbers are equal".Negative and zero are also accepted as inputs and the smallest number has to be found accordingly.In the Sample Input / Output provided, the highlighted text in bold corresponds to the input given by the user, and the rest of the text represents the output.Ensure to follow the object-oriented specifications provided in the question description.Ensure to provide the names for classes, attributes, and methods as specified in the question description.Adhere to the code template, if provided.Please do not use System.exit(0) to terminate the program.Sample Input 1:Enter the numbers587543Sample Output 1:The smallest number is 43Sample Input 2:Enter the numbers8-74Sample Output 2:The smallest number is -7Sample Input 3:Enter the numbers555Sample Output 3:All numbers are equalSample Input 4:Enter the numbers8878Sample Output 4:The smallest number is 8
Question
Olivia likes to play with numbers. She told her friends about a game in which Olivia would tell them three numbers and they would have to find the smallest among them. As it was time-consuming, her friends decided to write a program to get three numbers and print the smallest one. Help them by developing an application using your Java skills.Note:Use ternary operator.If all 3 numbers are same then print "All numbers are equal".Negative and zero are also accepted as inputs and the smallest number has to be found accordingly.In the Sample Input / Output provided, the highlighted text in bold corresponds to the input given by the user, and the rest of the text represents the output.Ensure to follow the object-oriented specifications provided in the question description.Ensure to provide the names for classes, attributes, and methods as specified in the question description.Adhere to the code template, if provided.Please do not use System.exit(0) to terminate the program.Sample Input 1:Enter the numbers587543Sample Output 1:The smallest number is 43Sample Input 2:Enter the numbers8-74Sample Output 2:The smallest number is -7Sample Input 3:Enter the numbers555Sample Output 3:All numbers are equalSample Input 4:Enter the numbers8878Sample Output 4:The smallest number is 8
Solution
Here is a simple Java program that fulfills the 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 numbers");
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
int num3 = scanner.nextInt();
if (num1 == num2 && num2 == num3) {
System.out.println("All numbers are equal");
} else {
int smallest = num1 < num2 ? (num1 < num3 ? num1 : num3) : (num2 < num3 ? num2 : num3);
System.out.println("The smallest number is " + smallest);
}
}
}
This program works as follows:
- It prompts the user to enter three numbers.
- It reads the three numbers from the user.
- If all three numbers are equal, it prints "All numbers are equal".
- If the numbers are not all equal, it uses the ternary operator to find the smallest number and prints it.
Similar Questions
Search course Type to search Minimum 3 characters required for search Close 1.1.1. Ternary Operator Nesting 16:17 Aiden is working on a complex decision-making program and wants to experiment with nesting ternary operators. Write a Java program that takes two numbers as input and uses nested ternary operators to determine and display whether the first number is greater, equal to, or less than the second number. Input format: The two lines of the input are integers. Output format: The output states whether the first number is Greater than/Equal to/Less than the second number or not as shown in displayed test cases. Note: The code for handling inputs and outputs is already been given, your task is to fill in the required code. Sample Test Cases Test case 1 58 52 First·number·is·Greater·than·second·number⏎ Test case 2 145 186 First·number·is·Less·than·second·number⏎ Test case 3 10 10 First·number·is·Equal·to·second·number⏎ Explorer TernaryOperator.java Submit 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ⌄ ⌄ package q22488; import java.util.Scanner; public class TernaryOperator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int num1 = scanner.nextInt(); int num2 = scanner.nextInt(); a=(num1>num2)?((num1 ==num2)?)"Greater than":"Equal to"; System.out.println("First number is " + result + " second number"); scanner.close(); } } Reason for late submission Eucs.Learner.Contents.LateSubmissionModal.Input.Placeholder Please enter at least 15 characters
Thara is attending a math lecture, and her professor asks students to swap two numbers using a bitwise operator. The professor finds it difficult to check the logic of 50 students. So, he decided to write a Java program to get two numbers, first and second, from the user and swap the numbers. He is not good at programming. Help them by developing an application using your Java skills.Note:Use bitwise operator.Negative and zero are also accepted as inputs and numbers have to be swapped accordingly.In the Sample Input / Output provided, the highlighted text in bold corresponds to the input given by the user, and the rest of the text represents the output.Ensure to follow the object-oriented specifications provided in the question description.Ensure to provide the names for classes, attributes, and methods as specified in the question description.Adhere to the code template, if provided.Please do not use System.exit(0) to terminate the program.Sample Input 1:Enter the numbers8743Sample Output 1:Before swappingfirst = 87, second = 43After swappingfirst = 43, second = 87
Emma needs your help in deciding which of the two numbers is smaller. Create a program that takes two integers as input, identifies the minimum using a relational operator, and displays it.Input format :The input consists of two space-separated integers.Output format :The output prints the smallest of the given input numbers.
Jacob was tasked with writing a program to find the smallest number among three numbers, but he was instructed to write the program using the extern storage class. Assist him in accomplishing his task.Input format :The input consists of three space-separated integers: num1, num2, and num3.Output format :The output displays "Smallest number is " followed by the smallest of three numbers.Refer to the sample output for the formatting specifications.Code constraints :
Develop a Java program to identify the largest among four numbers (32, 29,34, 21) using appropriate control statement and operators.
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.