Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Program to find largest among three numbers using conditional statements.

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:

  1. We include the <stdio.h> header file to use printf and scanf functions.
  2. In the main function, we declare three integer variables num1, num2, and num3 to store the user input.
  3. We prompt the user to enter three numbers using printf.
  4. We read the user input using scanf and store them in the variables num1, num2, and num3.
  5. We use a series of if-else statements to compare the numbers and determine the largest among them.
  6. If num1 is greater than or equal to num2 and num3, it is the largest number, so we print it.
  7. If num2 is greater than or equal to num1 and num3, it is the largest number, so we print it.
  8. If neither of the above conditions is true, num3 must be the largest number, so we print it.
  9. 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:

  1. The program starts by importing the Scanner class from the java.util package. The Scanner class allows us to read input from the user.
  2. The program defines a public class named LargestNumber.
  3. Inside the class, the program defines the main method, which serves as the entry point of the program.
  4. 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).
  5. The program prompts the user to enter three numbers by printing the message “Enter three numbers: ” to the console using System.out.print().
  6. 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.
  7. 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.
  8. 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().
  9. 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.
  10. 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.
  11. 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.