write a program in Java to find the prime numbers from the given range a and b by non nested loop.
Answers
Answer:
public class Prime {
public static void main(String[] args) {
int low = 20, high = 50;
while (low < high) {
boolean flag = false;
for(int i = 2; i <= low/2; ++i) {
// condition for nonprime number
if(low % i == 0) {
flag = true;
break;
}
}
if (!flag && low != 0 && low != 1)
System.out.print(low + " ");
++low;
}
}
}
Explanation:
public class Prime {
public static void main(String[] args) {
int low = 20, high = 50;
while (low < high) {
boolean flag = false;
for(int i = 2; i <= low/2; ++i) {
// condition for nonprime number
if(low % i == 0) {
flag = true;
break;
}
}
if (!flag && low != 0 && low != 1)
System.out.print(low + " ");
++low;
}
}
}
Output
23 29 31 37 41 43 47
Hope it helps..