Write a program to check whether a number given by the user is a two-digit or three-digit number. (Use ternary operator) in java
Answers
import java.util.Scanner;
public class SmallestNumberExample1
{
public static void main(String[] args)
{
int a, b, c, smallest, 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 smallest number in a temp variable
temp=a<b?a:b;
//comparing the temp variable with c and storing the result in the variable names smallest
smallest=c<temp?c:temp;
//prints the smallest number
System.out.println("The smallest number is: "+smallest);
}
}
Answer:import java.util.Scanner;
public class SmallestNumberExample1
{
public static void main(String[] args)
{
int a, b, c, smallest, 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 smallest number in a temp variable
temp=a<b?a:b;
//comparing the temp variable with c and storing the result in the variable names smallest
smallest=c<temp?c:temp;
//prints the smallest number
System.out.println("The smallest number is: "+smallest);
}
}
Output: Enter the first number:
23
Enter the second number:
11
Enter the third number:
67
The smallest Number is: 11
Explanation: