In: Computer Science
I have to code the assignment below. I cannot get the program to work and I am not sure what i am missing to get the code to work past the input of the two numbers from the user. My code is listed under the assignment details. Please help!
Write a Java program that displays the prime numbers between A and B. Inputs: Prompt the user for the values A and B, which should be integers with B greater than A. Outputs: The output will be all the prime numbers between A and B. Each line of output will contain five prime numbers except for the last line, which may have less depending on the total number of primes identified. Code the program in Eclipse using a loop to calculate all prime numbers between A and B. Use the code below to determine if a number is prime. Each line of output will contain 5 prime numbers except the last line.
boolean isPrime = true
for (int divisor = 2; divisor <= number / 2; divisor++)
{
if (number %
divisor == 0) {
isPrime =
false;
break;
Test your program and it should display all prime numbers between 101 and 200.
My Code
import java.util.Scanner;
public class Ch5Project {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the value of A: ");
int number1 =
input.nextInt();
System.out.print("Enter the value of B: ");
int number2 =
input.nextInt();
final int NUMBER_OF_PRIMES = 21;
final int NUMBER_OF_PRIMES_PER_LINE = 4;
int count = 100;
int number = 102;
while (count < NUMBER_OF_PRIMES) {
boolean isPrime = true;
for (int divisor = 2; divisor <=
number / 2; divisor++) {
if (number %
divisor == 0) {
isPrime =
false;
break;
}
}
if (isPrime) {
count++;
if (count %
NUMBER_OF_PRIMES_PER_LINE == 0) {
System.out.println(number);
}
else
System.out.print(number + " ");
}
number++;
}
}
}
Errors in the given code
Corrected code
//importing Scanner class for reading user input import java.util.Scanner; public class Ch5Project { public static void main(String[] args) { //creating Scanner object to read user input Scanner input = new Scanner(System.in); //prompt user to enter two integers and read it using nextInt() method System.out.print("Enter the value of A: "); int number1 = input.nextInt(); System.out.print("Enter the value of B: "); int number2 = input.nextInt(); //define constant NUMBER_OF_PRIMES_PER_LINE and set it value 5 final int NUMBER_OF_PRIMES_PER_LINE = 5; //initialize number with number1 and count with 0 int number = number1, count = 0; //loop iterate through each number from number1 to number2 while (number < number2) { //set boolean isPrime to true boolean isPrime = true; //iterate through 2 to half of the number for (int divisor = 2; divisor <= number / 2; divisor++) { //if number has any factor,then it is not a prime if (number % divisor == 0) { isPrime = false; break; } } //if number doesn't have any factor,then increment count by 1 and print //the number if (isPrime) { count++; //if count is a multiple of 5,then print output in new line if (count % NUMBER_OF_PRIMES_PER_LINE == 0) { System.out.println(number); } //otherwise print in same line else System.out.print(number + " "); } number++; } } }
Screen shot of the code
Screen shot of the output