FreeLearningBlogs

free learning,tutorial,programs

View Blog

Responsive Ads Here

Wednesday, 27 April 2016

Java Largest Two Number Example

Program that will find Largest Number from given Two Numbers

Method 1


class LargeTwoNumber{

public static void main(String args[])
{
int a=10,b=20;

if(a>b){

System.out.println("Largest Two Number is= "+a);
}else{

System.out.println("Largest Two Number is ="+b);
}
}
}

Output

Largest Two Number is = 20


Method 2

class LargeTwoNumber{

public static void main(String args[])
{
int a=10,b=20;

int c = (a>b)?a:b;

System.out.print("Largest Two Number is = " + c);
}
}


Output



Largest Two Number is = 20

Method 3


import java.util.*;
class LargeTwoNumber{

public static void main(String args[])
{
int a,b,c;

Scanner in = new Scanner(System.in);
System.out.print("Enter a and b Number ==> ");
a = in.nextInt();
b = in.nextInt();
c = (a>b)?a:b;
System.out.print("Largest Two Number is = " + c);
}
}


Output



Enter a and b Number ==> 10 20
Largest Two Number is = 20

Method 4

import java.util.*;
class LargeTwoNumber{

public static void main(String args[])
{
int a,b,c;

a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);

c = (a>b)?a:b;

System.out.print("Largest Two Number is = " + c);
}
}

Output




Method 5

import java.util.*;
class LargeTwoNumber{
int a,b,c;
LargeTwoNumber()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a and b Number ==> ");
a = in.nextInt();
b = in.nextInt();
c = (a>b)?a:b;
}

void display()
{

System.out.println("Largest Two Number ==> " + c);
}
}
class main{
public static void main(String args[]){

LargeTwoNumber l = new LargeTwoNumber();
l.display();
}
}



Output



Enter a and b Number ==> 10 20
Largest Two Number is = 20

No comments:

Post a Comment