Write a program to store 10 numbers in an array. Create another array that contains the squares of all the elements in the first array. Display the new array.
Answers
Answer:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
int[] arr = new int[10];
int[] squares = new int[10];
Scanner sc = new Scanner(System.in);
System.out.println("Enter all the 10 numbes in the original array");
for(int i = 0; i < arr.length; i++){
arr[i] = sc.nextInt();
}
System.out.println("Original elements of the array");
for(int i = 0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
for(int i = 0; i < squares.length; i++){
squares[i] = arr[i]*arr[i];
}
System.out.println("\nAfter squaring all elements in the Original array");
for(int i = 0; i < arr.length; i++){
System.out.print(squares[i] + " ");
}
}
}
Explanation:
Answer:
import java.util.*;
public class Numbers
{
public static void main(String[] args)
{
int[] arr = new int[10];
int[] squares = new int[10];
Scanner in = new Scanner(System.in);
System.out.println("Enter all the 10 numbes in the original array");
for(int i = 0; i < arr.length; i++)
{
arr[i] = in.nextInt();
}
System.out.println("Original elements of the array");
for(int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
for(int i = 0; i < squares.length; i++)
{
squares[i] = arr[i]*arr[i];
}
System.out.println("\nAfter squaring all elements in the Original array");
for(int i = 0; i < arr.length; i++)
{
System.out.print(squares[i] + " ");
}
}
}
Explanation:
Hope It Helps!