OneCompiler

Groovy program to find largest of two numbers

752

Below program shows you how to find largest number from given 2 numbers in Groovy. In this program, we get inputs from user and shows largest number from given two numbers using if condition

Groovy program to find largest of two numbers

Scanner scan = new Scanner(System.in);
def x = scan.nextLine()
def y = scan.nextLine()

if (x == y) {
    println x +" and "+ y +" are equal" 
} else if (x > y) {
    println x +" is greater than "+ y 
} else {
    println x +" is less than "+ y 
}

Results

Example #1:

Enter two numbers:
30
30
30 is equal to 30

Example #2:

Enter two numbers:
20
30
20 is less than 30

Example #3:

Enter two numbers:
30
20
30 is greater than 20

Practice with more examples here by providing inputs in the STDIN section