Question

In: Computer Science

This assignment asks to create an array with 10 integer elements. Ask user to input 10...

This assignment asks to create an array with 10 integer elements. Ask user to input 10 integer values. going through all the array elements and find/print out all the prime numbers. Instructions: Only use for loops for this lab (no need to create methods or classes for this assignment). Put a comment on each line of your codes, explaining what each line of code does.

basic java, please

Solutions

Expert Solution

//TestCode.java
import java.util.Scanner;
public class TestCode  {
    public static void main(String[] args) {
        // Creating scanner object
        Scanner scan = new Scanner(System.in);
        boolean isPrime;
        int x;

        // Declaring an array with 10 ints
        int arr[] = new int[10];

        // Reading values from user
        System.out.println("Enter 10 integer values for array:");
        for(int i = 0;i<arr.length;i++){
            arr[i] = scan.nextInt();
        }

        // Looping through each value in array
        for(int i = 0;i<arr.length;i++){
            x = arr[i];

            // Assuming x is prime
            isPrime = true;

            // Loop through 2 to x-1
            for(int j = 2;j<x;j++){
                // if x is divisible by any of j
                if(x%j == 0) {
                    // Then setting isPrime to false
                    isPrime = false;
                }
            }

            // Printing value of x if it is a prime
            if(isPrime){
                System.out.println(x);
            }
        }
    }
}


Related Solutions

In Java:Implement a program that repeatedly asks the user to input apositive integer and...
In Java:Implement a program that repeatedly asks the user to input a positive integer and outputs the factorial of that input integer. Do not use recursion, solution should use stack.
Directions of assignment: - Create an array of words of size 10. - Prompt the User...
Directions of assignment: - Create an array of words of size 10. - Prompt the User to enter the 10 integers. Populate the array with the integers as they are entered. - You MUST use indexed addressing to traverse through the array. - Determine the maximum and the minimum values contained within the array and print them out. Can you see what I am doing wrong here, the last part (to find the min and max) I can't seem to...
IN JAVA Write a MAIN METHOD that asks for user input of a positive integer and...
IN JAVA Write a MAIN METHOD that asks for user input of a positive integer and a negative integer validates the inputs using a loop and then calls the METHOD from Question 3 and prints the result. The MAIN should have two variables (appropriately typed), get the input from the user and store them in the variables after validating them, then call the method from question 3 (sending parameters, if necessary) and print the returned value with an appropriate descriptive...
Design a program that will ask the user to input two integer numbers and then perform...
Design a program that will ask the user to input two integer numbers and then perform the basic arithmetic operations such as addition and subtraction. Each calculation is done by a separate function. The main function gets the input from the user, then calls the addition function and the subtraction function one at a time to perform the calculations. Each calculation function (addition or subtraction function) performs an arithmetic operation and then returns the calculation results back to where it...
Create a small program that contains the following. ask the user to input their name ask...
Create a small program that contains the following. ask the user to input their name ask the user to input three numbers check if their first number is between their second and third numbers
• Write a C++ program that asks the user to input two integer values, then calls...
• Write a C++ program that asks the user to input two integer values, then calls a void function "swap" to swap the values for the first and second variable. • As we mentioned before, in order to swap the valors of two variables, one can use the following: temp= variable1; variable1 = variable2; variable2 = temp; • Display the two variables before you call swap and after you call that function. Comment in code would be greatly appreciated to...
Write a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
Write C++ program to do the following: 1. Create integer array size of 10 2. Ask...
Write C++ program to do the following: 1. Create integer array size of 10 2. Ask user input the values of the array's element using for loop 3. pass the array to void function. in void function do the following: a. Find the maximum of the array. b. Compute the element average c. Find out how many numbers are above the average d. Find out and print how many numbers are below the average e. find out how many numbers...
. Create a Python function that asks the user for a number (integer). The function should...
. Create a Python function that asks the user for a number (integer). The function should then tell the user how many hundreds can go into the number, and how much is left over. Hint: the % operator calculates the remainder of a division. For example, 10 % 3 gives a result 1. Hint2: Deal with the positive and negative values in separate parts of an if-else structure. Get the calculation work for positive values first. For negative values, make...
In this assignment we will be working with both text strings and a user input integer...
In this assignment we will be working with both text strings and a user input integer array from the data segment. The goal of the assignment is to have the user choose the number of items that they want to have in an array (we will keep it a small number for now, 1-10) and then enter those values which we will store into our array. We then want to allow them to search for a specific item in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT