Question

In: Computer Science

how to get this to not consider 0, 1 and negative integers as prime. maybe using...

how to get this to not consider 0, 1 and negative integers as prime. maybe using a "nested loop" or more than one loop?

//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); } } } }

Solutions

Expert Solution

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 = x > 1;
            // 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

1) How many positive integers are greater than 140, less than 30800 and relatively prime to...
1) How many positive integers are greater than 140, less than 30800 and relatively prime to 280.
Consider an array of length n containing positive and negative integers in random order. Write the...
Consider an array of length n containing positive and negative integers in random order. Write the C++ code that rearranges the integers so that the negative integers appear before the positive integers. write a program that includes both functions and a main() function that tests them. Name the two functions rearrangeN() and rearrangeN2().
Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0.
This program is for C.Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0. Combine the elements of A + B to create two- dimensional array C = A + B. Display array A, B and C to the screen for comparison. (Note a[0] + b[0] = c[0], a[1] + b[1] = c[1], etc.)
how many integers from 0 through 999,999 contain the digit 4 exactly twice? how many integers...
how many integers from 0 through 999,999 contain the digit 4 exactly twice? how many integers from 1 through 1000000 contain the digits 6 at least once
Consider the following statements, [ 0 , 1 ] × [ 0 , 1 ] with...
Consider the following statements, [ 0 , 1 ] × [ 0 , 1 ] with the dictionary order is complete. [ 0 , 1 ] × [ 0 , 1 ) with the dictionary order is complete. [ 0 , 1 ) × [ 0 , 1 ] with the dictionary order is complete. Where the dictionary order on R × R is given by ( a , b ) < ( x , y ) if either a...
Assuming integers are represented as 32-bit words and negative numbers are represented using the 2's complimentary...
Assuming integers are represented as 32-bit words and negative numbers are represented using the 2's complimentary method convert the following decimal numbers to hexadecimal numbers (show your work). a. -1314, b. 2020
Assuming integers are represented as 16-bit words and negative numbers are represented using the 2's complementary...
Assuming integers are represented as 16-bit words and negative numbers are represented using the 2's complementary method, convert the following hexadecimal numbers to decimal numbers a. 0xCAFE, b. 0x4DAD, c. 0xFACE
Let p be an integer other than 0, ±1. (a) Prove that p is prime if...
Let p be an integer other than 0, ±1. (a) Prove that p is prime if and only if it has the property that whenever r and s are integers such that p = rs, then either r = ±1 or s = ±1. (b) Prove that p is prime if and only if it has the property that whenever b and c are integers such that p | bc, then either p | b or p | c.
Consider that time could be internally represented as 3 integers. hour (between 0 - 23) minute...
Consider that time could be internally represented as 3 integers. hour (between 0 - 23) minute (between 0 - 59) second (between 0 - 59) MyTime class consists of the following members: Default constructor: set the time to 00:00:00 Constructor taking 3 integers i.e., hour, minute and second respectively. This constructor sets this object's Time based on the given parameters. Constructor taking 2 integers i.e., hour and minute respectively. This constructor sets this object's hour and minute based on the...
Solve the following equations in non-negative integers. 1. x2 - y2 = 221 2. a +...
Solve the following equations in non-negative integers. 1. x2 - y2 = 221 2. a + b = ab 3. gcd(a,b)lcm(a,b) = b + 9 4. x4 + 2x3 - y2(1+2x) + x2(1-y2) = 2299
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT