forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDivisible_Pairs_In_Array.java
46 lines (39 loc) · 1 KB
/
Divisible_Pairs_In_Array.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
/*
DIVISIBLE PAIRS IN AN ARRAY
The problem is to find all pairs of numbers in an array, such that
one of those numbers divides the other.
*/
import java.util.Scanner;
class Divisible_Pairs {
public static void main(String args[]) {
int num;
System.out.println("Enter the size of array : ");
Scanner s = new Scanner(System.in);
num = s.nextInt();
int a[] = new int[num];
System.out.println("Enter array elements");
for (int i = 0; i < num; i++) {
a[i] = s.nextInt();
}
System.out.println("The following are the divisible pairs :\n");
for (int i = 0; i < num; i++) {
for (int j = i + 1; j < num; j++) {
if (a[i] % a[j] == 0 || a[j] % a[i] == 0) {
System.out.println(a[i] + " " + a[j]);
}
}
}
}
}
/*
Input :
num = 4
Array = [1, 4, 5, 20]
Output :
The following are the divisible pairs :
1 4
1 5
1 20
4 20
5 20
*/