Prime numbers within a range

PHOTO EMBED

Sat Jul 10 2021 11:50:59 GMT+0000 (UTC)

Saved by @induja #java

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);
      }
    }
  }
}
    
    
  	
content_copyCOPY