Find Largest Among Three Numbers in Java

This is a simple java console program to find the largest among three numbers. Here conditional operator used to get largest numbers among from other numbers. Also ‘BufferedReader’ stream used to get input numbers from user.

Here class name is ‘largest’, so you should save your file with name ‘largest.java’.

import java.io.*;
class largest
{
  int a,b,c,larg,larg2;

void calc() throws IOException
 {
  BufferedReader Br = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("\nEnter 3 numbers : ");
   a = Integer.parseInt(Br.readLine());
   b = Integer.parseInt(Br.readLine());
   c = Integer.parseInt(Br.readLine());
   larg = a>b ? a : b;
   larg2 = larg>c ? larg : c;
  System.out.println("\nLargest among numbers " +  a +", "+ b + " and "+ c + " is : " + larg2);
  }
 public static void main(String s[]) throws IOException
  {
   largest l = new largest();
   l.calc();
  }
}

Sample output:

Enter 3 numbers:
25
46
17
Larges among numbers 25, 46 and 17 is : 46

Find Largest Among Three Numbers in Java




No comments:

Post a Comment