//Reading an integer & printing prime numbers upto that integer import java.io.*; import java.util.Scanner; import java.lang.Math; class Prime{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); int n =sc.nextInt(); int i; for(i = 2;i <= n; i++){ if(isprime(i) == 1){ System.out.print(i+" "); } } sc.close(); } static int isprime(int n){ int i; if(n==0 || n==1){ return(0); } if(n==2||n==3){ return(1); } if(n%2==0||n%3==0){ return(0); } for(i=5;i<Math.sqrt(n);i+=6){ if(n%i==0||n%(i+2)==0){ return(0); } } return(1); } }
Output:
25 2 3 5 7 11 13 17 19 23