Write a program that takes three numbers as input and determines the largest among them using conditional statements.
Program in C
C
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 >= num2 && num1 >= num3) {
printf("%d is the largest number.\n", num1);
} else if (num2 >= num1 && num2 >= num3) {
printf("%d is the largest number.\n", num2);
} else {
printf("%d is the largest number.\n", num3);
}
return 0;
}
Explanation:
- We include the <stdio.h> header file to use printf and scanf functions.
- In the main function, we declare three integer variables num1, num2, and num3 to store the user input.
- We prompt the user to enter three numbers using printf.
- We read the user input using scanf and store them in the variables num1, num2, and num3.
- We use a series of if-else statements to compare the numbers and determine the largest among them.
- If num1 is greater than or equal to num2 and num3, it is the largest number, so we print it.
- If num2 is greater than or equal to num1 and num3, it is the largest number, so we print it.
- If neither of the above conditions is true, num3 must be the largest number, so we print it.
- Finally, we return 0 to indicate successful execution of the program.
Output:
Output
Enter three numbers: 4
2
6
6 is the largest number.
Program in Java
Java
import java.util.Scanner;
public class LargestNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter three numbers: ");
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
int num3 = scanner.nextInt();
if (num1 >= num2 && num1 >= num3) {
System.out.printf("%d is the largest number.\n", num1);
} else if (num2 >= num1 && num2 >= num3) {
System.out.printf("%d is the largest number.\n", num2);
} else {
System.out.printf("%d is the largest number.\n", num3);
}
scanner.close();
}
}
Explanation:
- The program starts by importing the Scanner class from the java.util package. The Scanner class allows us to read input from the user.
- The program defines a public class named LargestNumber.
- Inside the class, the program defines the main method, which serves as the entry point of the program.
- Within the main method, an instance of the Scanner class is created by initializing it with System.in, which represents the standard input stream (keyboard input).
- The program prompts the user to enter three numbers by printing the message “Enter three numbers: ” to the console using System.out.print().
- The entered numbers are read from the user using the nextInt() method of the Scanner class and stored in the variables num1, num2, and num3.
- The program then compares the three numbers to find the largest one. It uses a series of if and else if conditions along with logical operators (>=) to determine the largest number.
- If num1 is greater than or equal to both num2 and num3, it means num1 is the largest number. In this case, the program prints the message “%d is the largest number.\n”, where %d is a placeholder for the value of num1, using System.out.printf().
- If the first condition is false, the program moves to the next else if condition. It checks if num2 is greater than or equal to both num1 and num3. If true, it means num2 is the largest number, and the program prints the corresponding message.
- If both the conditions are false, it means num3 must be the largest number. In this case, the program executes the else block and prints the corresponding message.
- Finally, the scanner object is closed using the close() method to release system resources.
Output
Enter three numbers: 4
2
6
6 is the largest number.