Squaring in java: In the previous article, we have seen Java Program to Subtract an Element to Every Other Elements of the Array
In this article we are going to see how to find square of each element of the array by using Java Language. How to find the square of array elements in java and How to square an array in java is discussed in the below information in detail manner. Squaring in java, Squaring a number in java, Square of elements in java are mentioned.
Java Program to Square Each Element of the Array
How to square in java: 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 find square each element of the array.
- By Static Initialization of Array Elements
- By Dynamic Initialization of Array Elements
- By Using Math.pow() function
Method-1: Java Program to Square Each Element of the Array By Static Initialization of Array Elements
Approach:
- Declare and initialize an array.
- Iterate each element of the array and multiply it with itself and replace the new value.
- Then print the new array.
Program:
public class Main { public static void main(String[] args) { //array initialized int arr[] = {10,20,30,40,50,60}; //iterating the array for(int i=0;i<arr.length;i++) { // finding square of array element by multiply the element with itself // and replacing the old value with new value arr[i] = arr[i]*arr[i]; } //printing the result array System.out.println("New array after finding square of each element :"); for(int i=0;i<arr.length;i++) { System.out.print(arr[i]+" "); } } }
Output: New array after finding square of each element : 100 400 900 1600 2500 3600
Method-2: Java Program to Square Each 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.
- Iterate each element of the array and multiply it with itself and replace the new value.
- Then print the new array.
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]; // take input from user for array elements System.out.print("Enter array elements: "); for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } //iterating the array for(int i=0;i<arr.length;i++) { // finding square of array element by multiply the element with itself // and replacing the old value with new value arr[i] = arr[i]*arr[i]; } //printing the result array System.out.println("New array after finding square of each element : "); for(int i=0;i<arr.length;i++) { System.out.print(arr[i]+" "); } } }
Output: Enter the size of array: 6 Enter array elements: 4 6 2 3 7 5 New array after finding square of each element : 16 36 4 9 49 25
Method-3: Java Program to Square Each Element of the Array By Using Math.pow() function
Approach:
- Take the array size as user input.
- Then take array elements as user input.
- Iterate each element of the array and find the square of array element by using
Math.pow()
function. - Then print the new array.
Program:
import java.util.*; 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 double arr[] = new double[n]; // take input from user for array elements System.out.print("Enter array elements: "); for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } //iterating the array for(int i=0;i<arr.length;i++) { // finding square of array element by using inbuilt function pow() // and replacing the old value with new value arr[i] = Math.pow(arr[i],2); } //printing the result array System.out.println("New array after finding square of each element : "); for(int i=0;i<arr.length;i++) { System.out.print(arr[i]+" "); } } }
Output: Enter the size of array: 4 Enter array elements: 2 3 4 5 New array after finding square of each element : 4.0 9.0 16.0 25.0
Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.
Related Java Programs: