## Nov 22, 2021

### Java Program to Print Square of Given Array Elements

In previous post we learnt how to print multiplication of given array elements. In this post we will learn how to print squares of given array elements. So let's get started... :)

### Step by step logic of the given program:

1. Accept array limit from user store it in variable say lim.

2. Accept elements from user. Run a for loop from to arr.length-1 and store elements one by one in array:

``````for(int i =0;i<arr.length;i++) {
arr[i]=sc.nextInt();
}``````

3. After that store Square of array elements one by one in variable called square and print it.

``````for(int i=0;i<arr.length;i++) {
square = arr[i]*arr[i];
System.out.println("Square of "+ arr[i] +" is: "+square);
}``````

## Java Program to Print Square of Given Array Elements :

``````

import java.util.Scanner;

public class ArraySquareExample {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int square=0;
//Accept limit
System.out.println("How many elements you want to enter: ");
int lim = sc.nextInt();
int[] arr= new int[lim];
//Accepting elements
System.out.println("Enter "+lim+" elements: ");
for(int i =0;i<arr.length;i++) {
arr[i]=sc.nextInt();
}
//Logic to print square of array elements
for(int i=0;i<arr.length;i++) {
square = arr[i]*arr[i];
System.out.println("Square of "+ arr[i] +" is: "+square);
}

}
}
``````

Output of the above program is:

How many elements you want to enter:
7
Enter 7 elements:
5
3
8
7
9
15
21
Square of 5 is: 25
Square of 3 is: 9
Square of 8 is: 64
Square of 7 is: 49
Square of 9 is: 81
Square of 15 is: 225
Square of 21 is: 441