Java'da asal sayı: asal sayı 1'den büyük olan ve yalnızca 1'e veya kendisine bölünen bir sayıdır. Yani asal sayılar kendisinden veya 1'den başka sayılara bölünemez. Örneğin 2, 3, 5, 7, 11, 13, 17.... asal sayılardır.
Not: 0 ve 1 asal sayı değildir. 2 tek çift asal sayıdır çünkü diğer tüm çift sayılar 2'ye bölünebilir.
Java'daki asal sayı programını görelim. Bu Java programında bir sayı değişkeni alıp bu sayının asal olup olmadığını kontrol edeceğiz.
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+' is not prime number'); }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+' is not prime number'); }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('Enter a number : '); int n = s.nextInt(); if (isPrime(n)) { System.out.println(n + ' is a prime number'); } else { System.out.println(n + ' is not a prime number'); } } 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('Enter the first number : '); int start = s.nextInt(); System.out.print('Enter the second number : '); int end = s.nextInt(); System.out.println('List of prime numbers between ' + start + ' and ' + 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'da Yöntem Kullanan Asal Sayı Programı
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+' is not prime number'); }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('Enter a number : '); int n = s.nextInt(); if (isPrime(n)) { System.out.println(n + ' is a prime number'); } else { System.out.println(n + ' is not a prime number'); } } 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('Enter the first number : '); int start = s.nextInt(); System.out.print('Enter the second number : '); int end = s.nextInt(); System.out.println('List of prime numbers between ' + start + ' and ' + 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'da Asal Sayı Programı (Başka bir yol)
Sayının önceden tanımlanmadığı bir yöntemi de kullanabilirsiniz. Burada sayının asal olup olmadığını kontrol etmek için kullanıcının sayıyı girmesi gerekir.
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('Enter a number : '); int n = s.nextInt(); if (isPrime(n)) { System.out.println(n + ' is a prime number'); } else { System.out.println(n + ' is not a prime number'); } } 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('Enter the first number : '); int start = s.nextInt(); System.out.print('Enter the second number : '); int end = s.nextInt(); System.out.println('List of prime numbers between ' + start + ' and ' + 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></=>=>=m;i++){>=m;i++){>