Write a program to array elements print all Positive number


This program takes an input array from the user and then prints out the positive elements of the array. Here's how it works:

  • The program prompts the user to enter the array limit (the number of elements in the array).
  • It then creates an integer array of size l to hold the input elements.
  • The program prompts the user to enter the elements of the array one by one.
  • The program then loops through the array elements and checks if each element is positive (greater than 0).
  • If an element is positive, it prints out that element.
  • Finally, the program ends.

Note: This program does not handle invalid user inputs such as non-numeric inputs or negative array limits. It also does not handle the case where the array has no positive elements.

Source Code

import java.util.Scanner;
class ArrayNum_Positive
{
	public static void main(String[] args)
	{   
		Scanner input =new Scanner(System.in);
		System.out.print("Enter the Array Limit :");
		int l =input.nextInt();
		int [] a =new int[l];
		for(int i=0;i<l;i++)
		{
			System.out.printf("Element of a[%d] :",i);
			a[i]=input.nextInt();
		}
		System.out.println("\nPositive Array Elements...\n");
		for(int p:a)
		{
			if(p>0)
				System.out.println(p);
		}
    }
}
 

Output

Enter the Array Limit :5
Element of a[0] :67
Element of a[1] :-4
Element of a[2] :3
Element of a[3] :-5
Element of a[4] :44

Positive Array Elements...

67
3
44

Example Programs