In: Computer Science
In Java Write a method called findMax that accepts three floating-point number as parameters and returns the largest one.(hints: use conditional statement in the method)
// java program to find big number
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scrr = new Scanner(System.in);
// scanning three numbers
System.out.print("Input the 1st number: ");
float num1 = scrr.nextFloat();
System.out.print("Input the 2nd number: ");
float num2 = scrr.nextFloat();
System.out.print("Input the 3rd number: ");
float num3 = scrr.nextFloat();
System.out.println("The greatest: "+findMax(num1,num2,num3));
}
// function to find big
public static float findMax(float p,float q,float r)
{
if (p > q)
{
if (q > r)
return p;
}
if (q > p)
{
if (q > r)
return q;
}
if (r > p)
{
if (r > q)
return r;
}
// if all are distinct returns Float.MIN_VALUE
return Float.MIN_VALUE;
}
}