Answers for "ternary operator in java to find biggest of 3 numbers"

2

Java program to find smallest of three numbers using ternary operator

// Java program to find smallest of three numbers using ternary operator
import java.util.Scanner;
public class SmallestOfThreeNumbers 
{
   public static void main(String[] args) 
   {
      int number1, number2, number3, smallest, temp;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter first number: ");
      number1 = sc.nextInt();
      System.out.println("Please enter second number: ");
      number2 = sc.nextInt();
      System.out.println("Please enter third number: ");
      number3 = sc.nextInt();
      sc.close();
      temp = number1 < number2 ? number1 : number2;
      smallest = number3 < temp ? number3 : temp;
      System.out.println("Smallest of three numbers using ternary operator: " + smallest);
   }
}
Posted by: Guest on February-22-2021
2

Java program to find largest in three numbers using ternary operator

// Java program to find largest in three numbers using ternary operator
import java.util.Scanner;
public class LargestInThree 
{
   public static void main(String[] args) 
   {
      int number1, number2, number3, largest;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter first number: ");
      number1 = sc.nextInt();
      System.out.println("Please enter second number: ");
      number2 = sc.nextInt();
      System.out.println("Please enter third number: ");
      number3 = sc.nextInt();
      largest = number3 > (number1 > number2 ? number1 : number2) ? number3 : ((number1 > number2) ? number1 : number2);
      System.out.println("Largest in three numbers using ternary operator: " + largest);
      sc.close();
   }
}
Posted by: Guest on November-02-2020

Code answers related to "ternary operator in java to find biggest of 3 numbers"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language