Program Perulangan Bercabang "Bilangan Prima"
Nama : Rayhan Qalby Ramadhan
NPM : 21082010008
Source Code :
package perulangan;
import java.util.Scanner;
/**Bilangan Prima
* @author Rayhan Qalby Ramadhan
* NPM 21082010008
*/
public class LoopBersarang {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int BanyakBilPrima, hitungAngka = 0, angka = 2;
System.out.println("Program Menampilkan N Bilangan Prima Pertama");
System.out.println("============================================");
//Memasukkan Banyaknya Bilangan Prima
System.out.print("Masukkan Banyaknya Bilangan Prima = ");
BanyakBilPrima = sc.nextInt();
System.out.println(BanyakBilPrima + " Bilangan prima pertama\t\t= ");
//Proses
while (hitungAngka < BanyakBilPrima){
boolean bilanganPrima = true;
for (int a = 2; a <= angka /2; a++){
if(angka % a ==0){
bilanganPrima = false;
break;
}
}
if (bilanganPrima == true){
hitungAngka++;
//Output N Bilangan Prima Pertama
System.out.print(angka + " ");
}
angka = angka + 1;
}
}
}
INPUT :
Hasil OUTPUT :
Tabel Tracing :
Comments
Post a Comment