25.5k views
3 votes
Write a program whose inputs are three integers, and whose outputs are the largest of the three values and the smallest of the three values. If the input is 7 15 3, the output is: largest: 15 smallest: 3 Your program should define and call two functions: Function LargestNumber(integer num1, integer num2, integer num3) returns integer largestNum Function SmallestNumber(integer num1, integer num2, integer num3) returns integer smallestNum The function LargestNumber should return the largest number of the three input values. The function SmallestNumber should return the smallest number of the three input values.

2 Answers

2 votes

Final answer:

To find the largest and smallest of three integers, define two functions: LargestNumber and SmallestNumber.

Step-by-step explanation:

To write a program that finds the largest and smallest of three integers, you can define two functions: LargestNumber and SmallestNumber. The LargestNumber function will compare the three input values and return the largest one, while the SmallestNumber function will return the smallest one. Here's an example in Python:

def LargestNumber(num1, num2, num3):
return max(num1, num2, num3)
def SmallestNumber(num1, num2, num3):
return min(num1, num2, num3)
# Example usage
input1 = 7
input2 = 15
input3 = 3
largest = LargestNumber(input1, input2, input3)
smallest = SmallestNumber(input1, input2, input3)
print('largest:', largest)
print('smallest:', smallest)

User Sumanchalki
by
4.4k points
3 votes

Answer:

import java.util.*;

public class Main

{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter the first number: ");

int n1 = input.nextInt();

System.out.print("Enter the second number: ");

int n2 = input.nextInt();

System.out.print("Enter the third number: ");

int n3 = input.nextInt();

System.out.println("largest: " + LargestNumber(n1, n2, n3));

System.out.println("smallest: " + SmallestNumber(n1, n2, n3));

}

public static int LargestNumber(int num1, int num2, int num3){

int largestNum = 0;

if(num1>=num2 && num1>=num3)

largestNum = num1;

else if(num2>=num1 && num2>=num3)

largestNum = num2;

else if(num3>=num1 && num3>=num2)

largestNum = num3;

return largestNum;

}

public static int SmallestNumber(int num1, int num2, int num3){

int smallestNum = 0;

if(num1<=num2 && num1<=num3)

smallestNum = num1;

else if(num2<=num1 && num2<=num3)

smallestNum = num2;

else if(num3<=num1 && num3<=num2)

smallestNum = num3;

return smallestNum;

}

}

Step-by-step explanation:

Create function called LargestNumber that takes three parameters, num1, num2, num3. Find the largest number using if else structure among them and return it. For example, if num1 is both greater than or equal to num2 and num3, then it is the largest one.

Create function called SmallestNumber that takes three parameters, num1, num2, num3. Find the smallest number using if else structure among them and return it. For example, if num1 is both smaller than or equal to num2 and num3, then it is the smallest one.

Inside the main, ask the user for the inputs. Call the functions and print the largest and smallest.

User Daniel Gratzer
by
4.5k points