logo

Java의 소수 프로그램

자바의 소수: 소수 은 1보다 크고 1 또는 자기 자신으로만 나누어진 숫자입니다. 즉, 소수는 자신이나 1이 아닌 다른 숫자로 나눌 수 없습니다. 예를 들어 2, 3, 5, 7, 11, 13, 17...이 소수입니다.

참고: 0과 1은 소수가 아닙니다. 나머지 짝수는 모두 2로 나누어질 수 있으므로 2만이 유일한 짝수이다.

Java의 소수 프로그램을 살펴보겠습니다. 이 Java 프로그램에서는 숫자 변수를 사용하여 숫자가 소수인지 여부를 확인합니다.

 public class PrimeExample{ public static void main(String args[]){ int i,m=0,flag=0; int n=3;//it is the number to be checked m=n/2; if(n==0||n==1){ System.out.println(n+&apos; is not prime number&apos;); }else{ for(i=2;i<=m;i++){ if(n%i="=0){" system.out.println(n+' is not prime number'); flag="1;" break; } if(flag="=0)" { end of else < pre> <span> Test it Now </span> <p>Output:</p> <pre> 3 is prime number </pre> <h2>Prime Number Program using Method in Java</h2> <pre> public class PrimeExample2{ static void checkPrime(int n){ int i,m=0,flag=0; m=n/2; if(n==0||n==1){ System.out.println(n+&apos; is not prime number&apos;); }else{ for(i=2;i<=m;i++){ if(n%i="=0){" system.out.println(n+' is not prime number'); flag="1;" break; } if(flag="=0)" { end of else public static void main(string args[]){ checkprime(1); checkprime(3); checkprime(17); checkprime(20); < pre> <span> Test it Now </span> <p>Output:</p> <pre> 1 is not prime number 3 is prime number 17 is prime number 20 is not prime number </pre> <h2>Prime Number Program in Java (Another way)</h2> <p>You can also use a method where number is not predefined. Here, user has to put the number to check if the number is prime.</p> <pre> import java.util.Scanner; import java.util.Scanner; public class PrimeExample3 { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print(&apos;Enter a number : &apos;); int n = s.nextInt(); if (isPrime(n)) { System.out.println(n + &apos; is a prime number&apos;); } else { System.out.println(n + &apos; is not a prime number&apos;); } } public static boolean isPrime(int n) { if (n <= 1) { return false; } for (int i="2;" < math.sqrt(n); i++) if (n % 0) true; pre> <p>Output:</p> <strong>Use image PrimeExample1</strong> <h2>Find prime numbers between two numbers</h2> <p>You can also find prime numbers between two specified numbers.</p> <pre> import java.util.Scanner; public class PrimeExample4 { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print(&apos;Enter the first number : &apos;); int start = s.nextInt(); System.out.print(&apos;Enter the second number : &apos;); int end = s.nextInt(); System.out.println(&apos;List of prime numbers between &apos; + start + &apos; and &apos; + end); for (int i = start; i <= end; i++) { if (isprime(i)) system.out.println(i); } public static boolean isprime(int n) (n <="1)" return false; for (int i="2;" % 0) true; pre> <p>Output:</p> <strong>Use image PrimeExample2</strong> </=></pre></=></pre></=m;i++){></pre></=m;i++){>

Java에서 메소드를 사용하는 소수 프로그램

 public class PrimeExample2{ static void checkPrime(int n){ int i,m=0,flag=0; m=n/2; if(n==0||n==1){ System.out.println(n+&apos; is not prime number&apos;); }else{ for(i=2;i<=m;i++){ if(n%i="=0){" system.out.println(n+\' is not prime number\'); flag="1;" break; } if(flag="=0)" { end of else public static void main(string args[]){ checkprime(1); checkprime(3); checkprime(17); checkprime(20); < pre> <span> Test it Now </span> <p>Output:</p> <pre> 1 is not prime number 3 is prime number 17 is prime number 20 is not prime number </pre> <h2>Prime Number Program in Java (Another way)</h2> <p>You can also use a method where number is not predefined. Here, user has to put the number to check if the number is prime.</p> <pre> import java.util.Scanner; import java.util.Scanner; public class PrimeExample3 { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print(&apos;Enter a number : &apos;); int n = s.nextInt(); if (isPrime(n)) { System.out.println(n + &apos; is a prime number&apos;); } else { System.out.println(n + &apos; is not a prime number&apos;); } } public static boolean isPrime(int n) { if (n <= 1) { return false; } for (int i="2;" < math.sqrt(n); i++) if (n % 0) true; pre> <p>Output:</p> <strong>Use image PrimeExample1</strong> <h2>Find prime numbers between two numbers</h2> <p>You can also find prime numbers between two specified numbers.</p> <pre> import java.util.Scanner; public class PrimeExample4 { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print(&apos;Enter the first number : &apos;); int start = s.nextInt(); System.out.print(&apos;Enter the second number : &apos;); int end = s.nextInt(); System.out.println(&apos;List of prime numbers between &apos; + start + &apos; and &apos; + end); for (int i = start; i <= end; i++) { if (isprime(i)) system.out.println(i); } public static boolean isprime(int n) (n <="1)" return false; for (int i="2;" % 0) true; pre> <p>Output:</p> <strong>Use image PrimeExample2</strong> </=></pre></=></pre></=m;i++){>

Java의 소수 프로그램(다른 방법)

숫자가 미리 정의되지 않은 방법을 사용할 수도 있습니다. 여기서 사용자는 숫자를 입력하여 숫자가 소수인지 확인해야 합니다.

 import java.util.Scanner; import java.util.Scanner; public class PrimeExample3 { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print(&apos;Enter a number : &apos;); int n = s.nextInt(); if (isPrime(n)) { System.out.println(n + &apos; is a prime number&apos;); } else { System.out.println(n + &apos; is not a prime number&apos;); } } public static boolean isPrime(int n) { if (n <= 1) { return false; } for (int i="2;" < math.sqrt(n); i++) if (n % 0) true; pre> <p>Output:</p> <strong>Use image PrimeExample1</strong> <h2>Find prime numbers between two numbers</h2> <p>You can also find prime numbers between two specified numbers.</p> <pre> import java.util.Scanner; public class PrimeExample4 { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print(&apos;Enter the first number : &apos;); int start = s.nextInt(); System.out.print(&apos;Enter the second number : &apos;); int end = s.nextInt(); System.out.println(&apos;List of prime numbers between &apos; + start + &apos; and &apos; + end); for (int i = start; i <= end; i++) { if (isprime(i)) system.out.println(i); } public static boolean isprime(int n) (n <="1)" return false; for (int i="2;" % 0) true; pre> <p>Output:</p> <strong>Use image PrimeExample2</strong> </=></pre></=>