Computer Science, asked by MrTSR, 14 days ago

JAVA
• Write a program in Java to find the largest number among three number using ternary operator

Answers

Answered by MultipleAnswer
4

import java.util.Scanner;

public class LargestNumberExample1

{

public static void main(String[] args)

{

int a, b, c, largest, temp;

//object of the Scanner class

Scanner sc = new Scanner(System.in);

//reading input from the user

System.out.println("Enter the first number:");

a = sc.nextInt();

System.out.println("Enter the second number:");

b = sc.nextInt();

System.out.println("Enter the third number:");

c = sc.nextInt();

//comparing a and b and storing the largest number in a temp variable

temp=a>b?a:b;

//comparing the temp variable with c and storing the result in the variable

largest=c>temp?c:temp;

//prints the largest number

System.out.println("The largest number is: "+largest

 \\

Answered by Oreki
9

{\bf Co de}

   \texttt{public class TernaryOperator \{}\\\texttt{\hspace{1.5em} public static void main(String[] args) \{}\\\texttt{\hspace{3em} int a = 17, b = 31, c = 11;}\\\texttt{\hspace{3em} int largest = (a > b \&\& a > c) ? a : (b > c) ? b : c;}\\\texttt{\hspace{3em} System.out.println("Largest number - " + largest);}\\\texttt{\hspace{1.5em} \}}\\\texttt{\}}

{\bf Out put}

   \texttt{Largest number - 31}

Similar questions