Java Program to Add an Element to Every Other Elements of the Array

In the previous article, we have seen Java Program to Divide an Element to Every Other Elements of the Array

In this article we are going to see how we can add an element to every other elements of the array except itself by using Java Language.

Java Program to Add an Element to Every Other Elements 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 add an element to every other elements of the array except itself.

Method-1: Java Program to Add an Element to Every Element of the Array By Static Initialization of Array Elements

Approach:

  • Declare and initialize an array.
  • Enter the index of array element to find that specific element.
  • This array element  will be added with other array elements.
  • Iterate each element of the array and add that specific array element with others elements except self.

Program:

public class Main
{

    public static void main(String[] args) 
    {
        //array initialized
        int arr[] = {10,20,30,40,50,60};
        // creating new array with size of actual array 
        int result[]= new int[arr.length];
        
        System.out.println("Array length is : "+arr.length);
        
        //declaring arrauy index of the specified number 
        //which will be added with other array elements
        int num  = 2;
        
        //if the entered index(means specified number) exists in the array 
        //then only the specifed array element can be added with other array elements 
        if(num<arr.length)
        {
            //iterating the array
           for(int i=0;i<arr.length;i++)
           {
               // checking condition 
               // if array element is not the specified number 
               // then it will enter into the if block 
               // and the number will be added with other elements except itself 
               if(arr[i]!=arr[num])
               {
                    // adding the specifed array element with other array elements
                    result[i] = arr[i]+arr[num];
               }
           }
        }
        
        //assigning the specified number to the same index of result array
        //as the specified number will be same 
        result[num]=arr[num];
        
        //printing the result array 
        System.out.println("New array after addition of array with a specific array element : ");
        for(int i=0;i<result.length;i++)
        {
            System.out.print(result[i]+" ");
        }    
   }
}
Output:
Array length is : 6
New array after addition of array with a specific array element : 
40 50 30 70 80 90

Method-2: Java Program to Add an Element to Every Element of the Array By Dynamic Initialization of Array Elements

Approach:

  • Take the array size as user input.
  • Then take array elements as user input.
  • Enter the index of array element to find that specific element.
  • This array element  will be added with other array elements.
  • Iterate each element of the array and add that specific array element with others elements except self.

Program:

import java.util.Scanner;

public class Main
{

    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        
        // initialize array with size n
        int arr[] = new int[n];
        // creating new array with size of actual array 
        int result[]= new int[arr.length];
        
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextInt();
        }
        
        System.out.println("Array length is : "+arr.length);
        
        //taking input of array index
        System.out.print("Enter index of the element to be added : ");
        int num  = sc.nextInt();
        
        //if the entered index(means specified number) exists in the array 
        //then only the specifed array element can be added with other array elements 
        if(num<arr.length)
        {
            //iterating the array
           for(int i=0;i<arr.length;i++)
           {
               // checking condition 
               // if array element is not the specified number 
               // then it will enter into the if block 
               // and the number will be added with other elements except itself
               if(arr[i]!=arr[num])
               {
                    // adding the speciifed array element with other array elements
                    result[i] = arr[i]+arr[num];
               }
           }
        }
        
        //assigning the specified number to the same index of result array
        //as the specified number will be same 
        result[num]=arr[num];
        
        //printing the result array 
        System.out.println("New array after addition of array with a specific array element : ");
        for(int i=0;i<result.length;i++)
        {
            System.out.print(result[i]+" ");
        }    
   }
}
Output:

Enter the size of array: 10
Enter array elements: 10 200 30 400 50 600 70 800 90 1000
Array length is : 10
Enter index of the element to be added : 3
New array after addition of array with a specific array element : 
410 600 430 400 450 1000 470 1200 490 1400

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.

Related Java Programs: