FINDING THE GREATEST
OF TWO NUMBERS
ALGORITHM:
- Declare
two variables n and m of type int in method main().
- Declare
a variable named din of type DataInputStream,
a class available for handling input stream.
- Create
an object of type DataInputStream and store its reference in din.
- Get
the values of n and m by calling the method readLine() of din twice. Apply the
method parseInt() of Integer
wrapper class to store the input as integers in n and m.
- Compare
the values of n and m using if statement and
comparison operators ==, > or <
- If
n == m then print n and m are equal
- Otherwise, check if n > m
i.
If it is true, print n is greater
ii.
Else print m
is greater
PROGRAM:
import java.io.*;
public class BigofTwo {
public static void
main(String[] args) throws IOException
{
int n, m;
DataInputStream din = new
DataInputStream(System.in);
System.out.println("Enter
the fist Integer :");
n =
Integer.parseInt(din.readLine());
System.out.println("Enter
the second Integer :");
m =
Integer.parseInt(din.readLine());
System.out.println("The
Inputs are : " + n + " and " + m);
if(n == m)
System.out.println("Both
Integers are Equal.");
else if(n>m)
System.out.println(n +
"is Greater!");
else
System.out.println(m +
"is Greater!");
}
}
Sample
Run: