Header Ads

Function which takes in 2 numbers and returns the greater of those number

In this Java programming tutorial, we'll be exploring how to find the greater of two numbers. This simple yet fundamental task is essential for any programmer, and we'll demonstrate different methods to achieve this goal.




Code:

import java.util.*;

public class Greater {
    public static void compare(int a, int b) {
        if (a == b) {
            System.out.println("Both are equal");
        } else if (a > b) {
            System.out.println(a);
        } else {
            System.out.println(b);
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter two numbers: ");
        int a = sc.nextInt();
        int b = sc.nextInt();
        compare(a, b);
    }
}


Output:

Enter two numbers: 
20 30
30




Related Links:


Post a Comment

0 Comments