import java.util.*; import java.lang.Math; class prime { public static boolean is_prime(int n) { if (n < 2) { return false; } if (n == 2) { return true; } if (n % 2 == 0) { return false; } for (int i = 3; i < Math.sqrt(n) + 1; i += 2) { if (n % i == 0) { return false; } } return true; } public static void main(String args[]) { int start, end; Scanner sc = new Scanner(System.in); System.out.println("Enter start value of range:"); start = sc.nextInt(); System.out.println("Enter end value of range:"); end = sc.nextInt(); System.out.println("List of primes:"); for (int i = start; i <= end; i++) { if (is_prime(i)) { System.out.println(i); } } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter