Print all Negative Elements in an Array

Printing all negative elements in an array involves iterating through the array and checking if each element is less than 0. Negative numbers are then displayed as output.

package com.javacodepoint.array;

public class PrintNegativeElements {
	public static void main(String[] args) {
		int[] arr = { 10, -5, 20, -15, 30, -25, 40 };

		System.out.println("Negative elements in the array:");
		for (int num : arr) {
			if (num < 0) {
				System.out.print(num + " ");
			}
		}
	}
}

OUTPUT:
Negative elements in the array:
-5 -15 -25

Read Also: Print Odd/Even No. from an Array. | Sum of Two Arrays.

Java logical programs list


Java Basic Programs

Java String Programs

Java Miscellaneous Programs

Java Programs based on the Collection Framework

Leave a Reply

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