Computer Science, asked by barbie1138, 1 year ago

write a program in java to input 3 number and find the largest number between 3 number using if-else statement​

Answers

Answered by rakeshchennupati143
4

Program:

import java.util.*;

public class Main{

  public static void main(String args[]){

     Scanner sc = new Scanner(System.in);

     System.out.print("Enter 3 numbers : ");

     int num1 = sc.nextInt(),num2 = sc.nextInt(),num3 = sc.nextInt();

     if(num1>num2 && num1>num3){

        System.out.println("among "+num1+","+num2+","+num3+" the largest number is "+num1);

     }else if(num2>num3 && num2>num1){

        System.out.println("among "+num1+","+num2+","+num3+" the largest number is "+num2);

     }else if(num3>num2 && num3>num1){

        System.out.println("among "+num1+","+num2+","+num3+" the largest number is "+num3);

     }else if(num1==num2){

        System.out.println("among "+num1+","+num2+","+num3+" the largest number is "+num2);

     }else if(num2==num3){

        System.out.println("among "+num1+","+num2+","+num3+" the largest number is "+num3);

     }else if(num3==num1){

        System.out.println("among "+num1+","+num2+","+num3+" the largest number is "+num3);

     }

  }

}

Output:

Enter 3 numbers : 20

20

30

among 20,20,30 the largest number is 30

Explanation:

  • consider number taken into num1 and num2 and num3
  • the conditions are as follows
  • num1>num2 and num1>num3 ---- num1 is greatest
  • num2>num3 and num2>num1 ---- num2 is greatest
  • num3>num1 and num3>num2 ---- num3 is greatest
  • num1 == num2 then num1 or num2 will be taken to print it as greatest,you will get a doubt what if 2 numbers are equal and the third number is bigger than them, if you see clearly in above case it will be solved in num3>num1 and num3>num2 so then it will got that case leaving this case
  • num2 == num3 then num1 or num3 is taken as largest
  • num3 == num1 then num3 or num1 is taken as largest

------Hope you liked my program using else-if tho it is so big, if you liked it mark as brainliest, it would really help me.    :)

Similar questions