Develop a Java program to identify the largest among four numbers (32, 29,34, 21) using appropriate control statement and operators.
Question
Develop a Java program to identify the largest among four numbers (32, 29, 34, 21) using appropriate control statement and operators.
Solution
Sure, here is a simple Java program that identifies the largest among four numbers (32, 29, 34, 21) using if-else control statement and comparison operators.
public class Main {
public static void main(String[] args) {
// define the numbers
int num1 = 32, num2 = 29, num3 = 34, num4 = 21;
// assume the first number is the largest
int largest = num1;
// compare the first number with the second, third and fourth numbers
if (num2 > largest) {
largest = num2;
}
if (num3 > largest) {
largest = num3;
}
if (num4 > largest) {
largest = num4;
}
// print the largest number
System.out.println("The largest number is " + largest);
}
}
This program first assumes that the first number is the largest. Then it compares this number with the second, third, and fourth numbers. If it finds a number that is larger, it updates the largest
variable. Finally, it prints out the largest number.
Similar Questions
Write a C++ code to find largest among 4 numbers using the concept call by reference.
Write a program to display a menu driven facility to find the greatest of two numbers,three numbers and four numbers
You are given a positive integer X. Write a simple program to find the second largest digit in the given number X.
Which algorithm category is used to determine the largest among three different numbers?*1 pointa. Sequentialb. Conditional (Selection)c. Iterative (Iteration)
What needs to go into <condition> for the code to output:234for(int num = 2; <condition>; num++) { System.out.println(num);}num <= 4num > 4num < 4num == 4
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.