``````import java.io.*;
public class Prime{
public static void main(String[] args) throws IOException {
boolean isPrime = true;
System.out.print("Enter number: ");
if(input < 2){
isPrime = false;
}
if (input == 2) {
isPrime = true;
}
if (isPrime)
for (int y=3; y<=(input/2); y+=2)
if(input == 3){
isPrime = true;
break;
}
else if (input % y == 0)
{
isPrime = false;
break;
}
if (isPrime)
System.out.println("That is a prime number.");
else{
System.out.println("That is not a prime number.");
}

}

}``````

// i was able to determine whether it is a prime number or not. but my problem is i want to count how many prime number in a given range..
//exampe: the user gave range 10. there are 4 prime numbers: 2, 3, 5, and 7..
..//help me!

// i was able to determine whether it is a prime number or not.

Your program to find a prime number is incorrect.

Firstly the program written is not able to handle any prime number entered, you should correct that ex: Try entering 5 .....

Also follow good coding standatrds where necessary - Like including braces

And finding the numbers for a range would be easy using the logic.....

try this code. its result is correct for 10 i dont know for other nos..

``````public class Prime {
public static void primeNo(int x){
for (int i = 1; i <= x; i++){
int ctr = 0;
for(int y = 1; y<=i; y++){
if(i%y == 0){
ctr++;
}
}
if (ctr == 2){
System.out.println(i);
}
}
}

public static void main(String[] args) {
primeNo(10);
}
}``````

Yes thanks! It works!

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.