Prime numbers are 2,3,5,7,11,13,17,19....etc.
Here is the example to print prime numbers below 20.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class primeNumbers {
public static void main(String[] args) {
int i=0;
int num=0;
for(i=1;i<20;i++){
int count=0;
for(num=i;num>=1;num--){
if(i%num==0){
count=count+1;
}
}
if(count==2){
System.out.println(i);
}
}
}
}
Here is the example to print prime numbers below 20.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class primeNumbers {
public static void main(String[] args) {
int i=0;
int num=0;
for(i=1;i<20;i++){
int count=0;
for(num=i;num>=1;num--){
if(i%num==0){
count=count+1;
}
}
if(count==2){
System.out.println(i);
}
}
}
}
No comments:
Post a Comment