Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

โปรแกรม Java แสดงจำนวนเฉพาะในช่วง (Prime Numbers Between Intervals) ใช้ฟังก์ชัน


ในบทความนี้ เราจะทำความเข้าใจวิธี แสดงจำนวนเฉพาะ (Prime Numbers) ในช่วงที่กำหนด โดยใช้ฟังก์ชัน

ตัวอย่าง Input และ Output

Input:

Starting number : 1
Ending number : 75

Output:

The prime numbers between the interval 1 and 75 are: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73

อัลกอริธึม

ขั้นตอนที่ 1 - เริ่มต้น
ขั้นตอนที่ 2 - ประกาศตัวแปร my_high, my_low
ขั้นตอนที่ 3 - อ่านค่าจากผู้ใช้ หรือกำหนดค่าเริ่มต้น
ขั้นตอนที่ 4 - สร้างฟังก์ชัน IsPrime รับ int คืน boolean ตรวจสอบจำนวนเฉพาะ
ขั้นตอนที่ 5 - วนลูป while จาก my_low ถึง my_high เรียก IsPrime ถ้า true พิมพ์เลขนั้น
ขั้นตอนที่ 6 - เพิ่ม my_low ทีละ 1
ขั้นตอนที่ 7 - แสดงผลลัพธ์
ขั้นตอนที่ 8 - หยุด

ตัวอย่างที่ 1: รับค่าจากผู้ใช้

import java.util.Scanner;
public class PrimeNumber {
    public static void main(String[] args) {
        int my_high, my_low;
        System.out.println("Required packages have been imported");
        Scanner my_scanner = new Scanner(System.in);
        System.out.println("A reader object has been defined");
        System.out.print("Enter the starting number : ");
        my_low = my_scanner.nextInt();
        System.out.print("Enter an ending Number: ");
        my_high = my_scanner.nextInt();
        System.out.println("The prime numbers between the interval " + my_low + " and " + my_high + " are:");
        while (my_low < my_high) {
            if (IsPrime(my_low))
                System.out.print(my_low + " ");
            ++my_low;
        }
    }
    public static boolean IsPrime(int my_input) {
        boolean flag = true;
        for (int i = 2; i <= my_input / 2; ++i) {
            if (my_input % i == 0) {
                flag = false;
                break;
            }
        }
        return flag;
    }
}

Output

Required packages have been imported
A reader object has been defined
Enter the starting number : 1
Enter the ending number : 75
The prime numbers between the interval 1 and 75 are: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73

ตัวอย่างที่ 2: กำหนดค่าไว้ล่วงหน้า

public class PrimeNumber {
    public static void main(String[] args) {
        int my_high, my_low;
        my_low = 1;
        my_high = 75;
        System.out.println("The starting and ending numbers are defined as " + my_low + " and " + my_high);
        System.out.println("The prime numbers between the interval " + my_low + " and " + my_high + " are:");
        while (my_low < my_high) {
            if (IsPrime(my_low))
                System.out.print(my_low + " ");
            ++my_low;
        }
    }
    public static boolean IsPrime(int my_input) {
        boolean flag = true;
        for (int i = 2; i <= my_input / 2; ++i) {
            if (my_input % i == 0) {
                flag = false;
                break;
            }
        }
        return flag;
    }
}

Output

The starting and ending numbers are defined as 1 and 75
The prime numbers between the interval 1 and 75 are: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73