Java program of Array Square

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:-

Main.java

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]);
        }
    }
}

Recommended:-

  1. Array square
  2. Generate number using odd digits
  3. Alternate Number Difference
  4. Next Greatest number
  5. Mark Comparison
  6. Print the characters in descending order
  7. Vowels in a fishBowl
  8. Least offer
  9. Ascending and descending order
  10. Mail Domain
  11. Count repeating words
  12. Sentence – Convert to upper and lower
  13. Count consecutive repeating characters
  14. Zig zag Array
  15. Pass and Fail Count
  16. Search a Course
  17. Average and Grade Calculation
  18. String – Find and replace the character (first occurrence)
  19. Sort the first and second half of an array
  20. Retail Shop
  21. Palindrome
  22. Numerology
  23. InitCap
  24. Array Compatiblilty
  25. Sum of the maximum and the minimum element
  26. String Concatenation
  27. Find Average Age
  28. Login
  29.  

1 thought on “Java program of Array Square”

Leave a Comment

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

x