How do you write a function that outputs the largest and the smallest number amongst three integer inputs? Also, let the user choose whether to end the program or not (do while)?
import java.util.Scanner; public class Main { public static void main(String[] args) { findLargestAndSmallest(); } public static void findLargestAndSmallest(){ int num=0, max=0, min=0, i=1; char choose='n'; Scanner sc = new Scanner(System.in); do{ System.out.printf('Enter %d number : ',i++); num = sc.nextInt(); if((i-1) == 1){ max = min = num; continue; } if(num>max) { max = num; }else if(num<max && num<min) min = num; if(i>3){ System.out.printf('\nYou want to close the program,\nIf Yes then press : Y\nIf No then press : N\n'); choose = sc.next().charAt(0); } if(choose == 'Y' || choose =='y'){ System.out.printf('Max value is :%d and Min value is : %d',max,min); break; }else{ continue; } }while(true); } }
Output.
Enter 1 number : 34
Enter 2 number : 23
Enter 3 number : 12
You want to close the program,
If Yes then press : Y
If No then press : N
n
Enter 4 number : 0
You want to close the program,
If Yes then press : Y
If No then press : N
y
Max value is :34 and Min value is : 0
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Program
Output.