Sophie, an aspiring Java programmer, is embarking on an adventure to explore arrays. She wants to create a Java program that allows users to input elements into an array and then prints the array of the squares of the given array elements. Can you assist Sophie by providing the Java code for her array adventure? Input Format: An integer N represents the size of the array. N integers represent the elements of the array, separated by space. Output Format: Print the squares of the elements of the array.
Question
Sophie, an aspiring Java programmer, is embarking on an adventure to explore arrays. She wants to create a Java program that allows users to input elements into an array and then prints the array of the squares of the given array elements. Can you assist Sophie by providing the Java code for her array adventure?
Input Format:
An integer N represents the size of the array.
N integers represent the elements of the array, separated by space.
Output Format:
Print the squares of the elements of the array.
Solution
Sure, here is a simple Java program that accomplishes what Sophie wants:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read the size of the array
int N = scanner.nextInt();
// Initialize the array
int[] array = new int[N];
// Read the elements of the array
for (int i = 0; i < N; i++) {
array[i] = scanner.nextInt();
}
// Print the squares of the elements of the array
for (int i = 0; i < N; i++) {
System.out.println(array[i] * array[i]);
}
}
}
This program first reads the size of the array from the user. Then it initializes an array of that size. It then reads the elements of the array from the user. Finally, it iterates over the array, squares each element, and prints the result.
Similar Questions
Design a Java program that allows users to input a series of integers into an array and then sorts the array in ascending order. Implement the sorting algorithm using the bubble sort technique.Write a Java code that accomplishes the following tasks:Prompt the user to enter the number of elements in the array.Prompt the user to input each element of the array.Sort the array in ascending order using the bubble sort algorithm.Display the sorted array.Demonstrate the functionality of your program with a sample input/output scenario.Sample Input12 // No of elements 23 // Elements to store in an array674512900260725434129865Sample Output12 12 23 34 45 54 65 67 72 98 260 900
What will be the output of the following Java code? public class array_output { public static void main(String args[]) { int array_variable [] = new int[10]; for (int i = 0; i < 10; ++i) { array_variable[i] = i; System.out.print(array_variable[i] + " "); i++; } } }
answerWhat will be the output of the following Java program? class array_value { public static void main(String args[]) { int array_variable [] = new int[20]; for (int v = 10; v < 20; ++v) { array_variable[v] = v/2; array_variable[v]++; System.out.print(array_variable[v] + " "); v++; } } }Options10 12 14 16 1810 11 12 13 14 15 16 17 18 196 7 8 9 1010 11 12 13 14 15 16 17 18 19 20
import java.util.Scanner;public class Test { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the number of elements in the array"); int n = sc.nextInt(); //Declare the array int arr[ ]=new int[n]; //Get the elements from user System.out.println("Enter the elements of the array"); for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } //Code to find the sum of odd elements in the array int sum=findSumOddElements(); // invoke the method System.out.println("The sum is "+); } public int findSumOddElements() { int sum=0; for(int i=0;i<;i++) { if(()!=0) { sum=sum+arr[i]; } } ; //return statement }}
Read Array Size and ElementsWrite a Program to read the Array Size and Array Values from the User and Print Array Values?Constraints:Input :- First Line of Input Consists of One Integer Value (Array Size). Second Line of Input Consists of few Integer Values Separated by Spaces (Array Elements).Output :- Print the All Array Elements.Constraints :- NoExample:Input 1 : 8 86 210 33 73 53 93 848 48Output 1: 86 210 33 73 53 93 848 48 Input 2 : 6 1 2 3 4 5 6Output 2: 1 2 3 4 5 6 Input 3 : 5 5 10 88 2 5Output 3: 5 10 88 2 5
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.