This code enables the user to enter a number and finds the prime from 2 to the entered number..Please name it PrimeNumbers.java Hope it'll help u..:D
PrimeNumbers.java
/*
creator: jennyvi San sebastian
*/
import java.util.Scanner;
public class PrimeNumbers{
public static void main(String ivy[]){
int input = 0;
int counter;
int div;
String choice = null;
do{
Scanner keyIn = new Scanner(System.in);
System.out.print("Enter a number: ");
input = keyIn.nextInt();
int start = 2;
while (start <= input){
div = 2;counter = 0;
while (div <= start)
{
if (start % div == 0 && div==start)
{
counter++;
}
if (start % div == 0 && div!=start)
{
counter++;
}
div++;
}
if (counter == 1)
{
System.out.print(start + " ");
}
start++;
}
System.out.println();
System.out.println("\nDo you want to continue <YES/N0>: ");
choice = keyIn.next();
choice = choice.toUpperCase();
}while(choice.equals("YES"));
}
atong 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.