Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will create a Java program that does the following: 1) Create a Scanner to r

ID: 3751391 • Letter: Y

Question

You will create a Java program that does the following:

1) Create a Scanner to read user input

2) Create an array that can hold 5 integers

3) Use a for loop and the Scanner to collect 5 integers from the user

4) Write a for loop to print all the odd numbers the user entered on a single line

5) Write a for loop to print all the even numbers the user entered on a single line

6) Write a for loop to print all the numbers in reverse order. If the user entered 1, 2, 3, 4, and 5 the outpu twould be 5 4 3 2 1

7) Write a for loop to print the square root of all the numbers

8) Write a for loop to print all the numbers on one line with a bracket at the beginning and one at the end with each number separated by a space. If the user entered 1, 2, 3, 4, and 5 the output would be [12345]

9) Write a for loop to print the square of every number entered

10)Close the Scanner

Explanation / Answer

Below is your answer: -

CheggScanner.java

public class CheggScanner {

public static void main(String[] args) {

// 1) Create a Scanner to read user input

Scanner scan = new Scanner(System.in);

// 2) Create an array that can hold 5 integers

Integer arr[] = new Integer[5];

// 3) Use a for loop and the Scanner to collect 5 integers from the user

for (int i = 0; i < arr.length; i++) {

System.out.print("Enter #" + (i + 1) + ": ");

arr[i] = scan.nextInt();

}

// 4) Write a for loop to print all the odd numbers the user entered on

// a single line

System.out.println();

System.out.print("Odd Numbers Entered : ");

for (int i = 0; i < arr.length; i++) {

if (arr[i] % 2 != 0) {

System.out.print(arr[i] + " ");

}

}

// 5) Write a for loop to print all the even numbers the user entered on

// a single line

System.out.println();

System.out.print("Even Numbers Entered : ");

for (int i = 0; i < arr.length; i++) {

if (arr[i] % 2 == 0) {

System.out.print(arr[i] + " ");

}

}

// 6) Write a for loop to print all the numbers in reverse order.

// If the user entered 1, 2, 3, 4, and 5 the outpu twould be 5 4 3 2 1

System.out.println();

System.out.print("Numbers entered in reverse order : ");

for (int i = arr.length - 1; i >= 0; i--) {

System.out.print(arr[i] + " ");

}

// 7) Write a for loop to print the square root of all the numbers

System.out.println(" Square root of all numbers: ");

for (int i = 0; i < arr.length; i++) {

System.out.print("Square root of " + arr[i] + " is " + Math.sqrt(arr[i])+" ");

}

// 8) Write a for loop to print all the numbers on one line with a

// bracket at the beginning and one at the end with each number

// separated by a space. If the user entered 1, 2, 3, 4, and 5 the

// output would be [12345]

System.out.println(" Numbers Entered with brackets : ");

System.out.print("[");

for (int i = 0; i < arr.length; i++) {

System.out.print(arr[i] + " ");

}

System.out.print("]");

System.out.println();

// 9) Write a for loop to print the square of every number entered

System.out.println(" Square of all numbers: ");

for (int i = 0; i < arr.length; i++) {

System.out.print("Square of " + arr[i] + " is " + (int)Math.pow(arr[i], 2)+" ");

}

//10)Close the Scanner

scan.close();

}

}

Output

Enter #1: 21
Enter #2: 22
Enter #3: 23
Enter #4: 24
Enter #5: 25

Odd Numbers Entered : 21 23 25
Even Numbers Entered : 22 24
Numbers entered in reverse order : 25 24 23 22 21
Square root of all numbers:
Square root of 21 is 4.58257569495584
Square root of 22 is 4.69041575982343
Square root of 23 is 4.795831523312719
Square root of 24 is 4.898979485566356
Square root of 25 is 5.0

Numbers Entered with brackets :
[21 22 23 24 25 ]

Square of all numbers:
Square of 21 is 441
Square of 22 is 484
Square of 23 is 529
Square of 24 is 576
Square of 25 is 625

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote