Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Membuat Barisan Bilangan Prima Antara Dua Bilangan dengan Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @Developer : Erik Gunawan
* @Nickname : EXz
* @Phone : 0896 5332 0577
* @EMail : admin@erikgunawan.com
* @Gmail : erik.gunawan44@gmail.com
* @Website : www.erikgunawan.com
* @Facebook : facebook.com/revolutionist.exz
* @Twitter : twitter.com/_EXz
*/
public class BilanganPrima {
private static BufferedReader br = null;
public static void main(String[] args) {
System.out.println("==============================");
System.out.println("Membuat Barisan Bilangan Prima");
System.out.println("==============================");
System.out.println();
br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("Masukkan batas bilangan pertama: ");
int a = Integer.parseInt(br.readLine());
System.out.print("Masukkan batas bilangan terakhir: ");
int b = Integer.parseInt(br.readLine());
System.out.println("\nBarisan Bilangan Prima antara " + a + " dan " + b + ": ");
getPrima(a, b);
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
private static void getPrima(int batasAwal, int batasAkhir) {
String prima = "";
for (int i=batasAwal; i<=batasAkhir; i++) {
if (checkPrima(i))
prima += i + ", ";
}
System.out.println(prima.substring(0, prima.length()-2));
}
private static Boolean checkPrima(int angka) {
int i;
for (i=2; i<=angka-1; i++) {
if (angka % i == 0)
return false;
}
if (angka==i)
return true;
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.