-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnegativepositive.java
More file actions
35 lines (32 loc) · 986 Bytes
/
Copy pathnegativepositive.java
File metadata and controls
35 lines (32 loc) · 986 Bytes
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
package array;
import java.util.Scanner;
public class negativepositive {
public static void main(String[] args) {
int n;
Scanner s = new Scanner(System.in);
System.out.println("Enter number of elements: ");
n = s.nextInt();
int ar[] = new int[n];
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++) {
ar[i] = s.nextInt();
}
int negIndex = 0;
for (int i = 0; i < n; i++) {
if (ar[i] < 0) {
if (i != negIndex) {
int temp = ar[i];
ar[i] = ar[negIndex];
ar[negIndex] = temp;
}
negIndex++;
}
}
for (int i = 0; i < negIndex; i++) {
System.out.print(ar[i] + " ");
}
for (int i = negIndex; i < n; i++) {
System.out.print(ar[i] + " ");
}
}
}