Write a Java program to find Largest of two numbers?
import java.io.*;
class LargestTwo
{
public static void main(String args[]) throws IOException
{
int a,b;
String str;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the value for a: ");
str = br.readLine();
a = Integer.parseInt(str);
System.out.print("Enter the value for b: ");
str = br.readLine();
b = Integer.parseInt(str);
if (a>b)
System.out.print(a + " is the Largest value.");
else
System.out.print(b+ " is the Largest value.");
}
catch(NumberFormatException nfe)
{
System.out.print("Incorrect Value.");
}
}
}
Ouptut:
Enter the value for a: 12
Enter the value for b: 24
24 is the Largest value.
class LargestTwo
{
public static void main(String args[]) throws IOException
{
int a,b;
String str;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the value for a: ");
str = br.readLine();
a = Integer.parseInt(str);
System.out.print("Enter the value for b: ");
str = br.readLine();
b = Integer.parseInt(str);
if (a>b)
System.out.print(a + " is the Largest value.");
else
System.out.print(b+ " is the Largest value.");
}
catch(NumberFormatException nfe)
{
System.out.print("Incorrect Value.");
}
}
}
Ouptut:
Enter the value for a: 12
Enter the value for b: 24
24 is the Largest value.
Write a Java program to find Smallest of two numbers?
import java.io.*;
class SmallestTwo
{
public static void main(String args[]) throws IOException
{
int a,b;
String str;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the value for a: ");
str = br.readLine();
a = Integer.parseInt(str);
System.out.println("Enter the value for b: ");
str = br.readLine();
b = Integer.parseInt(str);
if (a<b)
System.out.println(a + " is the Smallest value.");
else
System.out.println(b+ " is the Smallest value ");
}
catch(NumberFormatException nfe)
{
System.out.println("Incorrect Value.");
}
}
}
Ouptut:
Enter the value for a: 12
Enter the value for b: 24
12 is the Smallest value.
class SmallestTwo
{
public static void main(String args[]) throws IOException
{
int a,b;
String str;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the value for a: ");
str = br.readLine();
a = Integer.parseInt(str);
System.out.println("Enter the value for b: ");
str = br.readLine();
b = Integer.parseInt(str);
if (a<b)
System.out.println(a + " is the Smallest value.");
else
System.out.println(b+ " is the Smallest value ");
}
catch(NumberFormatException nfe)
{
System.out.println("Incorrect Value.");
}
}
}
Ouptut:
Enter the value for a: 12
Enter the value for b: 24
12 is the Smallest value.
Write a Java program to find Smallest of three numbers?
import java.io.*;
class SmallestThree
{
public static void main(String args[]) throws IOException
{
int a,b,c;
String str;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the value for a: ");
str = br.readLine();
a = Integer.parseInt(str);
System.out.print("Enter the value for b: ");
str = br.readLine();
b = Integer.parseInt(str);
System.out.print("Enter the value for c: ");
str = br.readLine();
c = Integer.parseInt(str);
if ((a<b) && (a<c))
System.out.println(a + " is the Smallest value.");
else if ((b>a) && (b>c))
System.out.println(b+ " is the Smallest value ");
else
System.out.println(c + "is the Smallest value.");
}
catch(NumberFormatException nfe)
{
System.out.println("Incorrect Value.");
}
}
}
Ouptut:
Enter the value for a: 12
Enter the value for b: 24
Enter the value for c: 10
10 is the Smallest value.
class SmallestThree
{
public static void main(String args[]) throws IOException
{
int a,b,c;
String str;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the value for a: ");
str = br.readLine();
a = Integer.parseInt(str);
System.out.print("Enter the value for b: ");
str = br.readLine();
b = Integer.parseInt(str);
System.out.print("Enter the value for c: ");
str = br.readLine();
c = Integer.parseInt(str);
if ((a<b) && (a<c))
System.out.println(a + " is the Smallest value.");
else if ((b>a) && (b>c))
System.out.println(b+ " is the Smallest value ");
else
System.out.println(c + "is the Smallest value.");
}
catch(NumberFormatException nfe)
{
System.out.println("Incorrect Value.");
}
}
}
Ouptut:
Enter the value for a: 12
Enter the value for b: 24
Enter the value for c: 10
10 is the Smallest value.
Write a Java program to find largest of three numbers?
import java.io.*;
class LargestThree
{
public static void main(String args[]) throws IOException
{
int a,b,c;
String str;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the value for a: ");
str = br.readLine();
a = Integer.parseInt(str);
System.out.println("Enter the value for b: ");
str = br.readLine();
b = Integer.parseInt(str);
System.out.println("Enter the value for c: ");
str = br.readLine();
c = Integer.parseInt(str);
if ((a>b) && (a>c))
System.out.println(a + " is the Largest value.");
else if ((b>a) && (b>c))
System.out.println(b+ " is the Largest value ");
else
System.out.println(c + "is the Largest value." + c);
}
catch(NumberFormatException nfe)
{
System.out.println("Incorrect Value.");
}
}
}
Ouptut:
Enter the value for a: 12
Enter the value for b: 24
Enter the value for c: 10
24 is the Largest value.
class LargestThree
{
public static void main(String args[]) throws IOException
{
int a,b,c;
String str;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the value for a: ");
str = br.readLine();
a = Integer.parseInt(str);
System.out.println("Enter the value for b: ");
str = br.readLine();
b = Integer.parseInt(str);
System.out.println("Enter the value for c: ");
str = br.readLine();
c = Integer.parseInt(str);
if ((a>b) && (a>c))
System.out.println(a + " is the Largest value.");
else if ((b>a) && (b>c))
System.out.println(b+ " is the Largest value ");
else
System.out.println(c + "is the Largest value." + c);
}
catch(NumberFormatException nfe)
{
System.out.println("Incorrect Value.");
}
}
}
Ouptut:
Enter the value for a: 12
Enter the value for b: 24
Enter the value for c: 10
24 is the Largest value.
Write a Java program to find largest of two numbers using Ternary operator?
import java.io.*;
class LargestTwoTernary
{
public static void main(String args[]) throws IOException
{
int a,b;
String str;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the value for a: ");
str = br.readLine();
a = Integer.parseInt(str);
System.out.println("Enter the value for b: ");
str = br.readLine();
b = Integer.parseInt(str);
int big = (a>b) ? a:b;
System.out.println(big + " is the Largest value.");
}
catch(NumberFormatException nfe)
{
System.out.println(" Incorrect Value.");
}
}
}
Ouptut:
Enter the value for a: 12
Enter the value for b: 24
24 is the Largest value.
class LargestTwoTernary
{
public static void main(String args[]) throws IOException
{
int a,b;
String str;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the value for a: ");
str = br.readLine();
a = Integer.parseInt(str);
System.out.println("Enter the value for b: ");
str = br.readLine();
b = Integer.parseInt(str);
int big = (a>b) ? a:b;
System.out.println(big + " is the Largest value.");
}
catch(NumberFormatException nfe)
{
System.out.println(" Incorrect Value.");
}
}
}
Ouptut:
Enter the value for a: 12
Enter the value for b: 24
24 is the Largest value.
Write a Java program to find Smallest of two numbers using Ternary operator?
import java.io.*;
class SmallestTwoTernary
{
public static void main(String args[]) throws IOException
{
int a,b;
String str;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the value for a: ");
str = br.readLine();
a = Integer.parseInt(str);
System.out.println("Enter the value for b: ");
str = br.readLine();
b = Integer.parseInt(str);
int small = (a<b) ? a:b;
System.out.println(small + " is the Smallest value.");
}
catch(NumberFormatException nfe)
{
System.out.println(" Incorrect Value.");
}
}
}
Ouptut:
Enter the value for a: 12
Enter the value for b: 24
12 is the Smallest value.
class SmallestTwoTernary
{
public static void main(String args[]) throws IOException
{
int a,b;
String str;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the value for a: ");
str = br.readLine();
a = Integer.parseInt(str);
System.out.println("Enter the value for b: ");
str = br.readLine();
b = Integer.parseInt(str);
int small = (a<b) ? a:b;
System.out.println(small + " is the Smallest value.");
}
catch(NumberFormatException nfe)
{
System.out.println(" Incorrect Value.");
}
}
}
Ouptut:
Enter the value for a: 12
Enter the value for b: 24
12 is the Smallest value.
Write a Java program to find largest of three numbers using Ternary operator?
import java.io.*;
class LargestThreeTernary
{
public static void main(String args[]) throws IOException
{
int a,b,c;
String str;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the value for a: ");
str = br.readLine();
a = Integer.parseInt(str);
System.out.println("Enter the value for b: ");
str = br.readLine();
b = Integer.parseInt(str);
System.out.println("Enter the value for c: ");
str = br.readLine();
c = Integer.parseInt(str);
int large = ((a>b) && (a>c)) ? a : ((b>a) && (b>c)) ? b: c;
System.out.println(large+ " is the Largest value ");
}
catch(NumberFormatException nfe)
{
System.out.println("Incorrect Value. ");
}
}
}
Ouptut: Enter the value for a: 12
Enter the value for b: 24
Enter the value for c: 10
24 is the Largest value.
class LargestThreeTernary
{
public static void main(String args[]) throws IOException
{
int a,b,c;
String str;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the value for a: ");
str = br.readLine();
a = Integer.parseInt(str);
System.out.println("Enter the value for b: ");
str = br.readLine();
b = Integer.parseInt(str);
System.out.println("Enter the value for c: ");
str = br.readLine();
c = Integer.parseInt(str);
int large = ((a>b) && (a>c)) ? a : ((b>a) && (b>c)) ? b: c;
System.out.println(large+ " is the Largest value ");
}
catch(NumberFormatException nfe)
{
System.out.println("Incorrect Value. ");
}
}
}
Ouptut: Enter the value for a: 12
Enter the value for b: 24
Enter the value for c: 10
24 is the Largest value.
Write a Java program to find Smallest of three numbers using Ternary operator?
import java.io.*;
class SmallestThreeTernary
{
public static void main(String args[]) throws IOException
{
int a,b,c;
String str;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the value for a: ");
str = br.readLine();
a = Integer.parseInt(str);
System.out.println("Enter the value for b: ");
str = br.readLine();
b = Integer.parseInt(str);
System.out.println("Enter the value for c: ");
str = br.readLine();
c = Integer.parseInt(str);
int small = ((a<b) && (a<c)) ? a : ((b<a) && (b<c)) ? b: c;
System.out.println(small+ " is the Smallest value ");
}
catch(NumberFormatException nfe)
{
System.out.println(" Incorrect Value.");
}
}
}
Ouptut: Enter the value for a: 12
Enter the value for b: 24
Enter the value for c: 10
10 is the Smallest value.
class SmallestThreeTernary
{
public static void main(String args[]) throws IOException
{
int a,b,c;
String str;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the value for a: ");
str = br.readLine();
a = Integer.parseInt(str);
System.out.println("Enter the value for b: ");
str = br.readLine();
b = Integer.parseInt(str);
System.out.println("Enter the value for c: ");
str = br.readLine();
c = Integer.parseInt(str);
int small = ((a<b) && (a<c)) ? a : ((b<a) && (b<c)) ? b: c;
System.out.println(small+ " is the Smallest value ");
}
catch(NumberFormatException nfe)
{
System.out.println(" Incorrect Value.");
}
}
}
Ouptut: Enter the value for a: 12
Enter the value for b: 24
Enter the value for c: 10
10 is the Smallest value.