-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestPosInteger.java
More file actions
59 lines (42 loc) · 1.51 KB
/
LargestPosInteger.java
File metadata and controls
59 lines (42 loc) · 1.51 KB
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
import java.util.Scanner;
public class LargestPosInteger {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int size;
int[] num;
System.out.print("Enter the size of array : ");
size = scanner.nextInt();
System.out.println();
num = new int[size];
for(int i=0;i<size;i++){
System.out.printf("Enter the element at index %d : ",i);
num[i] = scanner.nextInt();
while(num[i]==0){
System.out.println("'0' cannot be an element.Enter again!");
System.out.printf("Enter the element at index %d : ",i);
num[i] = scanner.nextInt();
}
}
System.out.print("\nYour array is : ");
for (int i : num) {
System.out.print(i + " ");
}
int max = -1;
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < num.length; j++) {
if (num[i] == -num[j]) {
int absValue = Math.abs(num[i]);
if (absValue > max) {
max = absValue;
}
}
}
}
if (max > 0) {
System.out.print("\nLargest positive integer with its negative present : " + max);
} else {
System.out.print("\n No such positive-negative pair found.");
}
scanner.close();
}
}