Separate Positive and Negative Numbers in Array Java

Program description:- Write a Java program to separate positive and negative numbers in a given array. Display those separated arrays. 0 should be treated as a positive number.

If the array contains only negative or only positive numbers then it should display the appropriate message, and display the original array.

Example1:-
Array = {-10, 5, 0, -9, 18, 27, -36}
Positive numbers = {5, 0, 18, 27}
Negative numbers = {-10, -9, -36}

Example2:-
Array = {9, 8, 7, 0, -2, 5}
Positive numbers = {9, 8, 7, 0, 0, 5}
Negative numbers = {-2}

Example3:-
Array = {-50, -60, -70}
The given array contains only negative numbers
Array = [-50, -60, -70]

Prerequisite:- Array in Java, find the length of an array in Java, different ways to print array in Java

Procedure to develop a Java method to separate positive and negative numbers from a given array,
a) Take an array.
b) Count the numbers of positive and negative numbers.
c) If any of them is not there then display some appropriate message, display the original array and return back to the caller method.
d) Create two arrays to store positive and negative numbers.
e) Traverse to the array
f) If the element is greater than or equal to zero (>=0) then insert them in the positive array.
g) Else insert them in the negative array.
h) Display both arrays.

In this program, we will take input value for the array from the end-user, but you can take it explicitly. See:- how to take array input in Java. We will use a method to perform the operation. 

Java Program to Separate Positive and Negative Numbers From a Given Array

import java.util.Arrays;
import java.util.Scanner;

public class ArrayTest {

  public static void main(String[] args) {
    // create Scanner class object to take input
    Scanner scan = new Scanner(System.in);

    // read size of the array
    System.out.print("Enter size of the array: ");
    int n = scan.nextInt();

    // create an int array of size n
    int numbers[] = new int[n];

    // take input for the array
    System.out.println("Enter array elements: ");
    for (int i = 0; i < n; ++i) {
      numbers[i] = scan.nextInt();
    }

    // display negative numbers
    seperate(numbers);

    // close Scanner
    scan.close();

  }

  // method to seperate +ve, -ve numbers
  public static void seperate(int[] numbers) {

    // variables
    int posCount = 0;
    int negCount = 0;
    int positive[] = null;
    int negative[] = null;

    // count positive and negative numbers
    for (int i : numbers) {
      if (i >= 0)
        ++posCount;
      else
        ++negCount;
    }

    // if array contains only positive or negative
    if (posCount == 0) {
      System.out.println("Array contains only negative numbers");
      // display array
      System.out.println("Array = " + Arrays.toString(numbers));
      return;
    } else if (negCount == 0) {
      System.out.println("Array contains only positive numbers");
      // display array
      System.out.println("Array = " + Arrays.toString(numbers));
      return;
    }

    // create positive and negative array
    positive = new int[posCount];
    negative = new int[negCount];

    // check element and insert
    int i = 0;
    int j = 0;
    for (int num : numbers) {
      if (num >= 0) {
        positive[i++] = num;
      } else {
        negative[j++] = num;
      }
    }

    // both arrays
    System.out.println("Negative numbers = " 
                      + Arrays.toString(negative));
    System.out.println("Positive numbers = " 
                      + Arrays.toString(positive));
  }
}

Output-

Enter size of the array: 7
Enter array elements:
-10 5 0 -9 18 27 -36
Negative numbers = [-10, -9, -36]
Positive numbers = [5, 0, 18, 27]

Enter size of the array: 6
Enter array elements:
9 8 7 0 -2 5
Negative numbers = [-2]
Positive numbers = [9, 8, 7, 0, 5]

Enter size of the array: 5
Enter array elements:
10 20 30 40 50
Array contains only positive numbers
Array = [10, 20, 30, 40, 50]

Enter size of the array: 3
Enter array elements:
-50 -60 -70
Array contains only negative numbers
Array = [-50, -60, -70]

There are multiple ways to display an array but here we will use Arrays.toString() method given in java.util.Arrays class which is used to convert an array to String format. The java.util.Arrays class contains many methods related to array operations like sort an array using sort()copy an array copyOf() or copyOfRange(), search an element in array using binary search, and e.t.c. Learn more about:- Arrays class and methods in Java.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply