The following program demonstrates how to Display All Prime Numbers in the Given Range in Java.
In this program, the outer for loop iterates for the entire range of 10 to 99. Whereas the inner for loop checks for each integer in the range. So, it divides the number by every integer ranging from 2 to half of the number. When any number divides, the given number is not prime. So, it sets the variable prime to false and exits from the inner loop. After that, the if statement checks the value of the variable prime. If it is true then the number is printed.
public class Exercise13
{
public static void main(String[] args) {
int a_number;
for(a_number=10;a_number<=99;a_number++)
{
boolean prime=true;
for(int i=2;i<=a_number/2;i++)
{
if(a_number%i==0)
{
prime=false;
break;
}
}
if(prime)
{
System.out.print(a_number+"\t");
}
}
}
}
Output