Search This Blog

Thursday, 13 November 2014

Java Program to find the Greatest of given Two Integers!

0 comments

FINDING THE GREATEST OF TWO NUMBERS

AIM:  To write a program in Java to find the greatest of two integers.

ALGORITHM:
  1. Declare two variables n and m of type int in method main().
  2. Declare a variable named din of type DataInputStream, a class available for handling input stream.
  3. Create an object of type DataInputStream and store its reference in din.
  4. 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.
  5. Compare the values of n and m using if statement and comparison operators ==, > or <
  6. If n == m then print n and m are equal
    1. 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:


Leave a Reply