Java Program to Print Positive Array Numbers

Write a Java Program to Print Positive Array Numbers with an example. Or how to write a Java Program to find and return the Positive items in a given array. In this Java positive array numbers example, we used the while loop to iterate ps_arr array and find the positive values (items greater than or equal to zero) and prints the same.

package ArrayPrograms;

public class PositiveArrayItems0 {
	
	public static void main(String[] args) {
		int j = 0;
		int[] ps_arr = {10, -9, 4, 11, -8, -13, 22, 19, -11, -99, 55, 18, -65, 100};	
		
		System.out.print("\nList of Positive Numbers in PS Array : ");
		while(j < ps_arr.length) 
		{
			if(ps_arr[j] >= 0) {
				System.out.format("%d ", ps_arr[j]);
			}
			j++;
		}
	}
}

Java Positive Array Numbers output

List of Positive Numbers in PS Array : 10 4 11 22 19 55 18 100 

Java Program to Print Positive Array Numbers using For Loop

This positive array items in Java example allows the user to enter the ps_arr array size and items.

package ArrayPrograms;

import java.util.Scanner;

public class PositiveArrayItems {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Size, i;		
		sc = new Scanner(System.in);	
		
		System.out.print("\nPlease Enter the PS Array size  : ");
		Size = sc.nextInt();
		int[] ps_arr = new int[Size];
		
		System.out.format("\nEnter PS Array %d elements : ", Size);
		for(i = 0; i < Size; i++) 
		{
			ps_arr[i] = sc.nextInt();
		}
		
		System.out.print("\nList of Positive Numbers in PS Array : ");
		for(i = 0; i < Size; i++) 
		{
			if(ps_arr[i] >= 0) {
				System.out.format("%d  ", ps_arr[i]);
			}
		}
	}
}
Java Program to Print Positive Array items 2

In this Java positive array items example, we created a separate void function PositiveElement to print the given array’s positive items.

package ArrayPrograms;

import java.util.Scanner;

public class PositiveArrayItems2 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int size, i;
		
		sc = new Scanner(System.in);		
		System.out.print("\nPlease Enter the PS Array size  : ");
		size = sc.nextInt();
		int[] ps_arr = new int[size];
		
		System.out.format("\nEnter PS Array %d elements : ", size);
		for(i = 0; i < size; i++) 
		{
			ps_arr[i] = sc.nextInt();
		}
		
		PositiveElement(ps_arr, size);
	}
	public static void PositiveElement(int[] ps_arr, int size ) {
		int i;
		
		System.out.print("\nList of Positive Numbers in PS Array : ");
		for(i = 0; i < size; i++) 
		{
			if(ps_arr[i] >= 0) {
				System.out.format("%d  ", ps_arr[i]);
			}
		}
	}
}

Java Positive Array elements output

Please Enter the PS Array size  : 9

Enter PS Array 9 elements : 10 -7 -99 0 4 -3 44 -67 100

List of Positive Numbers in PS Array : 10  0  4  44  100