import java.util.Scanner; public class MinofTwo { public static void main(String[] arg) { Scanner sc = new Scanner(System.in); int n=sc.nextInt(),a,b; while(n>0) { System.out.println((a=sc.nextInt())<(b=sc.nextInt())?a:b); n--; } } }
Tutorials, tips and tricks about java programming language. Answers for many doubts about java programming language. Answering many howtos. Sample java projects and source code, java games, java programs for java practical labs.
Search This Blog
Showing posts with label find smallest number. Show all posts
Showing posts with label find smallest number. Show all posts
Java Program to Find Minimum of Two Numbers
Java Program to compare three numbers and display the minimum of two numbers. Here, we will see a simple java program to display the smaller number among two input numbers. This program takes first input as the number of sets of numbers (a set contains two numbers whose minimum is to be displayed). Then it takes numbers as sets of 2 numbers. For every pair of numbers, the java program displays the smaller number. The following java program does it. It makes use of the ternary operator (conditional operator) and Scanner class in java.
Java Program to Find Minimum of Three
It is the basics of any programming language to compare three numbers and display the minimum of the three. Here, we will see a short java program to display the smallest number among three input numbers. This program takes first input as the number of sets of numbers (a set contains three numbers whose minimum is to be displayed). Then it takes numbers as sets of three numbers. for every three number, the java program displays the smallest of them. The following program does it. It makes use of the ternary operator (conditional operator) and Scanner class.
import java.util.Scanner; public class MinOfThree { public static void main(String[] arg) { Scanner sc = new Scanner(System.in); int n=sc.nextInt(),a,b,c; while(n>0) { if((a=sc.nextInt())>(b=sc.nextInt())) System.out.println(b<(c=sc.nextInt())?b:c); else System.out.println(a<(c=sc.nextInt())?a:c); n--; } } }
Subscribe to:
Posts (Atom)