-
Notifications
You must be signed in to change notification settings - Fork 61
/
FindTheSmallest.java
71 lines (67 loc) · 1.67 KB
/
FindTheSmallest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.java.numbers;
import java.util.Scanner;
/*
* FIND THE SMALLEST WIHTOUT USING < AND > SYMBOL
*
* Write a java program to find the smallest among a, b, c
* without using < AND > symbol.
*
* For comparing two numbers we need to use the relational operator.
*
* The alternate idea is to find the divisor of a / b,
* if result is 0 then a is small, else b is small
*
* Then apply same logic with the result like c / a or c / b
*
*/
public class FindTheSmallest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the value of a ::");
int a = scanner.nextInt();
System.out.println("Enter the value of b ::");
int b = scanner.nextInt();
System.out.println("Enter the value of c ::");
int c = scanner.nextInt();
int smallest = findTheSamllest(a, b, c);
System.out.println("The smallest of a,b,c is :: "+smallest);
scanner.close();
}
public static int findTheSamllest(int a,int b,int c){
if( a/b == 0){
//here a is smaller than b
if( c/a == 0){
//here c is smaller than a
return c;
}
else{
//here a is smaller than c
return a;
}
}else{
//here b is smaller a
if( c/b == 0){
//here c is smaller than b
return c;
}
else{
//here b is smaller than c
return b;
}
}
}
}
/*
Enter the value of a :: 20
Enter the value of b :: 10
Enter the value of c :: 30
The smallest of a,b,c is :: 10
Enter the value of a :: 200
Enter the value of b :: 100
Enter the value of c :: 55
The smallest of a,b,c is :: 55
Enter the value of a :: 30
Enter the value of b :: 50
Enter the value of c :: 70
The smallest of a,b,c is :: 30
*/