-
Help me get started
Hello guys below is the program that i need to create. I know how to do the prime number but got stuck in displaying a error message.
Plz help
A prime number is a positive integer that is evenly divisible only by 1 and itself. Thus, e.g. 2,3,5,7,11,13,17,19 are all prime numbers. A pair of numbers x and y are twin primes if x and y are prime and |x-y| = 2. In this assignment you are to write a program that generates the twin primes between 3 and some number entered by the user. Thus, your code should prompt the user for a positive integer greater than or equal to 3. If the user enters something that is not a number or not a positive integer greater than or equal to 3, then the user should be shown an error message and should be again prompted to enter a positive integer greater than or equal to 3. If, after 3 tries, the user fails to enter a positive integer greater than or equal to 3, your code should display an error message and exit. Your code should then report the twin primes between 3 and the number entered by the user. It should also report the number of twin primes between 3 and the number entered by the user. The following represents a possible run of your code:
Please enter a positive integer greater than or equal to 3
20
<3,5> <5,7> <11,13> <17,19> are twin primes between 3 and 20.
There are 4 twin prime pairs between 3 and 20.
In order to get your program to exit when the user fails to enter a correct value after 3 tries, use the follow piece of code:
System.exit(1);
At a minimum, you will need to have two methods - a main() method and an isPrime() method that has the following signature:
public static boolean isPrime(int num)
-
Re: Help me get started
Please help us help you by telling/showing us: What have you tried, and how is it not working? What specifically confuses you?
-
Re: Help me get started
package Twinprime;
import java.util.Scanner;
public class primenumber {
public static void main (String args []){
String primeNo = "";
int a;
int j = 0;
int LastPrime =1;
Scanner in = new Scanner(System.in);
System.out.println( "Enter first integer: " ); // prompt
a=in.nextInt(); // read first number from user
for (int i = 1; i < a;i++) {
for (j = 2; j < i; j++) {
if (i % j == 0) {
break;
}
}
if (i == j) {
primeNo += i + " ";
if ((i - LastPrime) == 2) {
System.out.print("("+(i-2)+","+i+")");
}
LastPrime = i;
}
}
}
}
According to question I can get twin prime...but can not figure it out where to put boolean...
any help will be appreciated
-
Re: Help me get started
It seems that you need to implement the boolean isPrime(int number) method which I don't see in your code attempt above. Why not give a go at making this method? Also, since you're new here you don't know how to use code tags yet, which if used help your posted code retain its formatting and be readable. Please place [code][/code] tags above and below your code blocks that you post here. Note that the top tag is different from the bottom one.