Perulangan Bersarang Bilangan Prima
RISMA PARAMESTI
22082010014
package bilanganprimapertama;
import java.util.Scanner;
public class Bilanganprimapertama {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int bilangan, i,check=0, banyak=0, n=0;
System.out.print("Masukkan bilangan : ");
bilangan=sc.nextInt();
for ( i = 1; i <= bilangan; i++) {
if (bilangan%i==0){
check++;
}
}
if (check==2 || check%2!=0){
System.out.println(bilangan + "adalah bilangan prima");
}else{
System.out.println(bilangan + "adalah bukan bilangan prima");
}
System.out.println("-------------------------------------------------");
System.out.print("Masukkan banyaknya angka bilangan prima : ");
banyak= sc.nextInt();
do{
for ( i = 2; i < n; i++) {
if (n%i==0){
break;
}
}if (i==n){
System.out.print(n + " ");
banyak-=1;
}n++;
}while (banyak>0);
System.out.println("");
}
}
Komentar
Posting Komentar