In: Computer Science
Develop an Algorithm and java program using Design Recipe for the following problems.
Draw a flowchart to compute the largest and smallest of 4 numbers
: Write a Java Program for the above flowchart, use methods(functions), and nested if-else statements. Take input from the user using the Scanner object. Use separate methods to compute the Largest and Smallest of the numbers. Method 1 Name: findLargest(param 1, param 2, param 3, param 4) Method 2 Name: findSmallest(param 1, param 2, param 3, param 4)
JAVA PROGRAM
import java.util.*;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num1,num2,num3,num4;
System.out.println("Enter Numbers: ");
//reads input from user
num1 = sc.nextInt();
num2 = sc.nextInt();
num3 = sc.nextInt();
num4 = sc.nextInt();
// checks the condition for largest number
if(num1 > num2 && num1 > num3 &&
num1 > num4)
{
System.out.println("Largest Number: "+num1);
}
else if(num2 > num1 && num2 > num3
&& num2 > num4){
System.out.println("Largets Number: "+num2);
}
else if(num3 > num1 && num3 > num2 && num3
> num4)
{
System.out.println("Largest Number: "+num3);
}
else{
System.out.println("Largest Number: "+num4);
}
//checks the condition for Smallest Numbers
if(num1 < num2 && num1 < num3 && num1 <
num4)
{
System.out.println("Smallest Number: "+num1);
}
else if(num2 < num1 && num2 < num3
&& num2 < num4)
{
System.out.println("Smallest Number: "+num2);
}
else if(num3 < num1 && num3 < num2 && num3
< num4)
{
System.out.println("Smallest Number: "+num3);
}
else
{
System.out.println("Smallest Number: "+num4);
}
}
}
OUTPUT