Java Program to Print all Negative Elements in an Array

Program description:- Write a Java program to print all negative elements in an array. Take an array, write a method to perform the operation. If the array doesn’t contain any negative number then it should display the appropriate message.

Example1:-
Array = {-15, -10, -5, 0, 5, 10, 15}
Negative numbers = {-15, -10, -5}

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

If the array doesn’t contain any negative number then it should display the appropriate message.

Example3:-
Array = {10, 20, 30, 40, 50}
Array doesn’t contain negative number.

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

Procedure to develop Java method to print all negative elements in the given array,
a) Take the array.
b) Take a flag variable of boolean type and initialize it with false.
c) Check the array contains any negative number? If no, then display an appropriate message like “given array doesn’t contain any negative number.” and return back to the caller method.
d) Else display each negative number.

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. 

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
    displayNegative(numbers);

    // close Scanner
    scan.close();

  }

  // method to display negative numbers
  public static void displayNegative(int[] numbers) {

    // variables
    boolean flag = false;
    int i = 0;

    // check is there any negative numbers?
    for (i = 0; i < numbers.length; i++) {
      if(numbers[i] < 0){
        flag = true;
        break;
      }
    }
        
    // -ve number is not available
    if(!flag) {
      System.out.println("Array doesn’t contain negative number.");
      return;
    }
    
    // display -ve numbers
    System.out.println("Negative numbers = ");
    for (; i < numbers.length; i++) {
      if(numbers[i] < 0) {
        System.out.print(numbers[i]+" ");
      }
    }
  }
}

Output:-

Enter the size of the array: 5
Enter array elements:
10 20 30 40 50
Array doesn’t contain a negative number.

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

Also See:- Find the Sum of Array in Java, Average in Java using Array, Sum of Two Arrays Elements, Compare Two Arrays in Java, Merge Two Arrays in Java, Merge Two Sorted Arrays, Copy Array 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 do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *