Java Program to print maximum among two numbers

Java Program to print maximum among two numbers

In this tutorial we will learn writing Java Program to print maximum number among given two numbers.

There are various ways to write this program in java like using inbuilt max() method or we can build our logic using if-else.

In this tutorial we will learn writing both ways.

Also to find maximum number among two, we can have integer or float or double variables. So we will also going to cover that part in this tutorial.

Program 1: Print maximum among two numbers in Java (Own Logic)

[java]
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the first integer number: “);
int num1 = sc.nextInt();
System.out.println(“Enter the second integer number: “);
int num2 = sc.nextInt();
if(num1>num2)
System.out.println(num1+” is maximum among two”);
else
System.out.println(num2+” is maximum among two”);
}
}
[/java]

Output

Program 2: Print maximum among two numbers in Java using max()

[java]
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the first integer number: “);
int num1 = sc.nextInt();
System.out.println(“Enter the second integer number: “);
int num2 = sc.nextInt();
System.out.println(Math.max(num1, num2)+” is the maximum among two”);
}
}
[/java]

Output