Write a Java program where you create a method to find a maximum number between two integers

The Java.lang.math.max() function is an inbuilt function in Java which returns maximum of two numbers. The arguments are taken in int, double, float and long.If a negative and a positive number is passed as argument then the positive result is generated. And if both parameters passed are negative then the number with the lower magnitude is generated as result.

Syntax:

dataType max(dataType num1, dataType num2) The datatypes can be int, float, double or long. Parameters : The function accepts two parameters num1 and num2 among which the maximum is returned

Return value:The function returns maximum of two numbers. The datatype will be the same as that of the arguments.

Given below are the examples of the function max()

public class Gfg {

    public static void main(String args[])

    {

        double a = 12.123;

        double b = 12.456;

        System.out.println(Math.max(a, b));

    }

}

Output:

12.456

public class Gfg {

    public static void main(String args[])

    {

        int a = 23;

        int b = -23;

        System.out.println(Math.max(a, b));

    }

}

Output:

23

public class Gfg {

    public static void main(String args[])

    {

        int a = -25;

        int b = -23;

        System.out.println(Math.max(a, b));

    }

}

Output:

-23

Article Tags :

This article is created to cover some programs in Java that is used to find and print the largest or biggest between two given numbers. Here are the list of ways used to do the job:

  • Find largest of two numbers using if...else
  • Find largest of two numbers using conditional operator
  • Find largest of two numbers using user-defined function

Largest of Two Numbers using if-else

The question is, write a Java program to find largest between of two numbers. Both the number must be received by user at run-time of the program. The program given below is its answer:

import java.util.Scanner; public class CodesCracker { public static void main(String[] args) { int numberOne, numberTwo, largest; Scanner scan = new Scanner(System.in); System.out.print("Enter the First Number: "); numberOne = scan.nextInt(); System.out.print("Enter the Second Number: "); numberTwo = scan.nextInt(); if(numberOne>numberTwo) largest = numberOne; else largest = numberTwo; System.out.println("\nLargest = " +largest); } }

The snapshot given below shows the sample run of above Java program, with user input 20 and 22 as two numbers:

Here is another sample run with user input 22 and 20. That is, 22 as first and 20 as second number:

But the problem with above program is, what if user enters same number, or a real number (containing decimal point). Therefore, I've modified the above program and created a new one to remove these two problems. Here is the program:

import java.util.Scanner; public class CodesCracker { public static void main(String[] args) { float a, b; Scanner scan = new Scanner(System.in); System.out.print("Enter Two Numbers: "); a = scan.nextFloat(); b = scan.nextFloat(); if(a>b) System.out.println("\nLargest between "+a+" and "+b+" is "+a); else if(b>a) System.out.println("\nLargest between "+a+" and "+b+" is "+b); else System.out.println("\nBoth Numbers are same."); } }

Here is its sample run with user input 123.42 as first and 1324.23 as second number:

Largest of Two Numbers using Conditional Operator

With the use of conditional operator, the program becomes shorter. That is, the conditional operator helps in finding the largest of two numbers using single statement as shown in the program given below:

import java.util.Scanner; public class CodesCracker { public static void main(String[] args) { int a, b, big; Scanner scan = new Scanner(System.in); System.out.print("Enter Two Numbers: "); a = scan.nextInt(); b = scan.nextInt(); big = (a>b) ? a : b; System.out.println("\nLargest = " +big); } }

Here is its sample run with user input 100 and 101 as two numbers:

Largest of Two Numbers using Function

This program uses a user-define function named LargestOfTwo(), that takes two numbers as its two arguments and returns the largest between its arguments.

import java.util.Scanner; public class CodesCracker { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Enter Two Numbers: "); int x = scan.nextInt(); int y = scan.nextInt(); System.out.println("\nLargest = " +LargestOfTwo(x, y)); } public static int LargestOfTwo(int a, int b) { return (a>b) ? a:b; } }

This program produces same output as of previous program.

Same Program in Other Languages

Java Online Test

« Previous Program Next Program »

class Maxoftwo { public static void main(String args[]) { //taking value as command line argument. //Converting String format to Integer value int i = Integer.parseInt(args[0]); int j = Integer.parseInt(args[1]); if(i> j) System.out.println(i+" is greater than "+j); else System.out.println(j+" is greater than "+i); } }

 OUTPUT:

C:\java>javac Maxoftwo.java C:\java>java Maxoftwo 10 20 20 is greater than 10 C:\java>java Maxoftwo 30 10 30 is greater than 10

Note: C:\>java> is a directory where source and class files exist.


Postingan terbaru

LIHAT SEMUA