java 生成两个数之间的素数


 

//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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM