Java Program to Double All the Negative Integers of the Array

In the previous article, we have seen Java Program to Double All the Positive Integers of the Array

In this article we will see how to double the negative elements of an array using Java Programming Language.

Java Program to Double All the Negative Integers of the Array

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to double all the negative elements of the array and print the new array.

Method-1: Java Program to Double All the Negative Integers of the Array By Static Initialization of Array Elements

Approach:

  • Take an array with elements in it.
  • Print the array elements.
  • Iterate the array elements.
  • Check the negative elements and double it then replace the new element in the array.
  • Print the array.

Program:

import java.util.Arrays;

public class Main
{
    public static void main(String args[])
    {
        // Creating the array
        int arr[] = {1, -2, 3, -4, 5};

        // Prints the array elements
        System.out.println("The array elements are"+Arrays.toString(arr));
       
        //iterates all the array elements and doubles the negative elements of the array.
        for(int i=0;i<arr.length;i++)
        {
            //checking if the array element is negative
            //then it will double the element
            if(arr[i]<0)
                arr[i]=arr[i]*2;
        }
        // Prints the sum
        System.out.println("After doubling all the negative elements of the array: "+Arrays.toString(arr));
    }
}
Output:

The array elements are[1, -2, 3, -4, 5]
After doubling all the positive elements of the array: [1, -4, 3, -8, 5]

Method-2: Java Program to Double All the Positive Integers of the Array By Dynamic Initialization of Array Elements

Approach:

  • Ask the user to enter the array size and store it.
  • Create an empty array of the specified size.
  • Ask the user to enter the elements.
  • Print the array elements.
  • Iterate the array elements.
  • Check the positive elements and double it then replace the new element in the array.
  • Print the array.

Program:

import java.util.*;

public class Main
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        // Taking size as input from the user
        System.out.println("Enter the array size : ");
        int size = scan.nextInt();
        // Creating the array
        int arr[] = new int[size];
        // Entering the array elements
        System.out.println("Enter array elements : ");
        for(int i=0;i<size;i++)
        {
            arr[i] = scan.nextInt();
        }

        // Prints the array elements
        System.out.println("The array elements are"+Arrays.toString(arr));
       
        //iterates all the array elemnts and doubles the negative elements of the array.
        for(int i=0;i<arr.length;i++)
        {
            //checking if the array element is negative
            //then it will double the element
            if(arr[i]<0)
                arr[i]=arr[i]*2;
        }
        // Prints the sum
        System.out.println("After doubling all the negative elements of the array: "+Arrays.toString(arr));
    }
}
Output:

Enter the array size : 
5
Enter array elements : 
10 -20 -30 40 50
The array elements are[10, -20, -30, 40, 50]
After doubling all the negative elements of the array: [10, -40, -60, 40, 50]

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: