Array Square Program in java

Question:

An array of N integers is passed as input to the program. The program must print another array where value at each index will be the sum of the square of all the values in the input array except the value at that index in the input array.

[Input format: First input refers to the no of elements in the array and the next is the series of elements in the array]

Sample Input1:

5

1

2

3

4

5

Sample Output1:

54

51

46

39

30

CODE:

import java.util.*;
public class Main 
{
    public static void main (String[] args) {
        int i=0;
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        
        int arr[]=new int[n];
        for(i=0;i<n;i++)
        {
            arr[i]=sc.nextInt();
        }
        
        int arr2[]=new int[n];
        for(i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(i!=j)
                {
                    arr2[i]+=arr[j]*arr[j];
                }
            }
        }
        
        for(i=0;i<n;i++)
        {
            System.out.println(arr2[i]);
        }
    }
}
  1. Generate number using odd digits
  2. Alternate Number Difference
  3. Next Greatest number

Leave a Comment

Scroll to Top