Dremendo Tag Line

Input numbers and print the largest in Java

if else if - Question 6

In this question, we will see how to input 3 unique integers and print the largest among them in Java programming using the if else if statement. To know more about if else if statement click on the if else if statement lesson.

Q6) Write a program in Java to input 3 unique integers and print the largest among them. Make use of logical operator && to solve this question.

Program

import java.util.Scanner;

public class Q6
{
    public static void main(String args[])
    {
        int a,b,c;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter 3 unique numbers");
        a=sc.nextInt();
        b=sc.nextInt();
        c=sc.nextInt();
        if(a==b || b==c || c==a)
        {
            System.out.print("Please enter 3 unique numbers");
        }
        else if(a>b && a>c)
        {
            System.out.print("Largest number is " + a);
        }
        else if(b>a && b>c)
        {
            System.out.print("Largest number is " + b);
        }
        else
        {
            System.out.print("Largest number is " + c);
        }
    }
}

Output

Enter 3 unique numbers
15
28
7
Largest number is 28
video-poster