Find Positive or Negative Number in array in Java

In this tutorial we Will be Going to Solve The Problem Java Program to find  Positive or Negative Number in an array. Also, we will be focusing on arrays and Positive-negative numbers during this tutorial.

If you don’t know How to Solve Problem Negative-Positive numbers in java, then you are at perfect Spot for clearing all the doubts and queries.

Introduction

Initially, we will be going to Understand What are Arrays and How are they initialized in java.




Also, Java Program to find Positive or negative numbers in the array would be in emphasis.

Basically, an array is a Data Structure for the collection of values of single or same datatype. The array can be of int, float or any other datatype.

The general form of array declaration is defined as below:

datatype variable_name[];

For example: int arr[];

The general form of new as it applies to one-dimensional arrays appears as follows:

variable_name = new type[size];

For example: int arr[]=new int [30];

Java Program to find Positive or Negative Number In the array

In this Solution, initially, We will be Taking user input of the size of array we will be Going to take input and then we will be creating or initializing an array with the same size we took the input.

It is shown below :

int [] a = new int[Size];

After the creation, we will be Taking the Input from User, i.e, list of all integers in the array from the user.

After this, inside the for loop, we will be giving a check to each Number and Check Whether it is Greater Than 0 or lesser than 0.

As the Check is made, we will be Displaying Accordingly whether It is Positive Number Or Negative Number. You will be displaying the message Negative or Positive number along with the Indexing element in an array.

public class Pono
{
  private static Scanner sc;
  public static void main(String[] args) 
  {
    int Size, i;
    sc = new Scanner(System.in);
    System.out.print(" Please Enter Number of elements in an array : ");
    Size = sc.nextInt();	
    int [] a = new int[Size];
    System.out.print(" Please Enter " + Size + " elements of an Array  : ");
    for (i = 0; i < Size; i++)
    {
      a[i] = sc.nextInt();
    }   

    for(i = 0; i < Size; i++)
    {
      if(a[i] >= 0)
      {
        System.out.println("Positive Number:"+a[i]);
      }
      else
      {
        System.out.println("Negative Number:"+a[i]);
      }
    }	
  }
}

Below Is The output Shown:

Find Positive or Negative Number in array in Java

Also read:


Leave a Reply

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