이 기사에서는 두 간격 사이에 소수를 표시하는 방법을 이해할 것입니다. 소수는 1과 자기 자신의 두 약수만 가지며 다른 숫자로 나눌 수 없는 특수 숫자입니다.
수는 인수가 1과 자기 자신뿐인 경우 소수입니다. 11은 소수입니다. 그 인수는 1과 11 자체입니다. 소수의 몇 가지 예는 2, 3, 5, 7, 11, 13 등입니다. 2는 유일한 짝수 소수입니다. 다른 모든 소수는 홀수입니다.
아래는 동일한 데모입니다 -
입력
입력이 -
라고 가정합니다.Starting number : 1 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
알고리즘
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
예시 1
여기에서 입력은 프롬프트에 따라 사용자가 입력하고 있습니다. 이 예제는 코딩 접지 도구에서 라이브로 사용해 볼 수 있습니다.
.
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;
}
}
} 출력
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
예시 2
여기에서 정수는 이전에 정의되었으며 그 값은 콘솔에 액세스되어 표시됩니다.
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;
}
}
} 출력
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