Computer >> Máy Tính >  >> Lập trình >> Java

Chương trình Java để hiển thị các số nguyên tố giữa hai khoảng thời gian

Trong bài này, chúng ta sẽ hiểu cách hiển thị số nguyên tố giữa hai khoảng. Số nguyên tố là số đặc biệt chỉ có hai thừa số 1 và chính nó và không thể chia cho bất kỳ số nào khác.

Một số là số nguyên tố nếu các thừa số duy nhất của nó là 1 và chính nó. 11 là một số nguyên tố. Các yếu tố của nó là 1 và 11 chính nó. Một số ví dụ về số nguyên tố là 2, 3, 5, 7, 11, 13, v.v. 2 là số nguyên tố chẵn duy nhất. Tất cả các số nguyên tố khác đều là số lẻ.

Dưới đây là một minh chứng về điều tương tự -

Đầu vào

Giả sử đầu vào của chúng tôi là -

Starting number : 1
Ending number : 75

Đầu ra

Đầu ra mong muốn sẽ là -

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

Thuật toán

Step 1 - START
Step 2 - Declare values namely
Step 3 - Read the required values from the user/ define the values
Step 4 - Run a while loop between the lower number and the higher number.
Step 5 - Run a for loop, iterate over each number between the intervals and check if the number is divisible by any of its lower numbers except 1. Store the values.
Step 6 - Display the result
Step 7 - Stop

Ví dụ 1

Ở đây, đầu vào đang được người dùng nhập dựa trên lời nhắc. Bạn có thể thử trực tiếp ví dụ này trong công cụ nền tảng mã hóa của chúng tôi Chương trình Java để hiển thị các số nguyên tố giữa hai khoảng thời gian .

import java.util.Scanner;
public class PrimeNumber {
   public static void main(String[] args) {
      int my_high, my_low, i;
      boolean my_temp;
      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) {
         my_temp = false;
         for(i = 2; i <= my_low/2; ++i) {
            if(my_low % i == 0) {
               my_temp = true;
               break;
            }
         }
         if (!my_temp && my_low != 0 && my_low != 1)
            System.out.print(my_low + " ");
         ++my_low;
      }
   }
}

Đầu ra

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 5 3 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73

Ví dụ 2

Ở đây, số nguyên đã được xác định trước đó và giá trị của nó được truy cập và hiển thị trên bảng điều khiển.

public class PrimeNumber {
   public static void main(String[] args) {
      int my_high, my_low, i;
      boolean my_temp;
      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) {
         my_temp = false;
         for(i = 2; i <= my_low/2; ++i) {
            if(my_low % i == 0) {
               my_temp = true;
               break;
            }
         }
         if (!my_temp && my_low != 0 && my_low != 1)
            System.out.print(my_low + " ");
           ++my_low;
      }
   }
}

Đầu ra

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