//print all prime numbers within a number

import java.util.Scanner;

public class Numbers {

    public static void main(String[] args) {
        int beg, end;
        Scanner ip = new Scanner(System.in);
        System.out.print("Enter the beginning value: ");
        beg = ip.nextInt();
        System.out.print("Enter the ending value: ");
        end = ip.nextInt();
        for (int i = beg; i <= end; i++) {
            if (isPrime(i))
                System.out.print(i + " ");
        }
        ip.close();
    }

    private static boolean isPrime(int num) {
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0)
                return false;
        }
        return true;
    }
}



OUTPUT:
Enter the beginning value: 50
Enter the ending value: 100
53 59 61 67 71 73 79 83 89 97

 

相关文章:

  • 2021-07-07
  • 2021-12-04
  • 2021-12-07
  • 2021-12-26
  • 2022-12-23
  • 2022-12-23
  • 2022-02-09
  • 2022-12-23
猜你喜欢
  • 2021-06-13
  • 2022-12-23
  • 2021-10-19
  • 2021-09-19
  • 2022-12-23
  • 2021-11-14
  • 2021-10-13
相关资源
相似解决方案