Let’s see a program to print prime numbers in java from 1 to n.
package programnew; public class PrintPrime { public static void main(String[] args) { for(int i=2;i<=100;i++) { boolean flag = true; for(int j=2;j<=i-1;j++) { if(i%j==0){ flag = false; break; } } if(flag==true) { System.out.print(i+" "); } } } }
Output is :- 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97