Write a program to input three integers and print the largest among the three integers using ternary operator. (ezie)
Answers
We will be solving this program using Java.
Program Códe :
import java.util.*;
class largest
{
public static void main(String args[])
{
int a = 0, b = 0, c = 0;
int max;
Scanner sc = new Scanner(System.in);
System.out.println("Enter three nos");
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
max = (a > b) ? (a > c) ? a : c : (b > c) ? b : c;
System.out.println("The largest number is"+" "+max);
}
}
Output :
Enter three nos
5
3
7
The largest number is 7
Knowledge Bytes :
- What is ternary operator?
Ternary operator is an operator which operate pn three operands. It is also called conditional operator because the value assigned to a variable depends upon a logical expression or a condition.
- Syntax:-
variable = (test condition) ? Expression 1 : Expression 2;
For example,
int a = 5, b = 3;
max = (a > b) ? a : b;
Here, the value of 5 is stored in max as the test condition (a > b) is true.
For better understanding refer to the attachment.
Answer:
import java.util.Scanner;
public class greatestbetween3number
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = in.nextInt();
System.out.print("Enter second number: ");
int b = in.nextInt();
System.out.print("Enter third number: ");
int c = in.nextInt();
int max = a > b ? a : b;
max = max > c ? max : c;
System.out.print("Largest Number = " + max);
}
}
Explanation:
Hope this helps please rate as brainliest