[java] menentukan bilangan prima atau bukan

METNUM

|Tugas METNUM: Halaman 130 no.4

Buatlah program yang meminta sebuah bilangan n dan menentukan apakah bilangan tersebut adalah bilangan prima atau bukan.

Jawab

//irwananwar.wordpress.com
import java.util.Scanner;
public class loop4_prima {
	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		System.out.printf("Input sebuah bilangan : ");
		int n=s.nextInt();
		int b = 0;
		for(int i=1;i<=n;i++){
		if(n%i==0)
			b=b+1;	}
		if(b==2)
			System.out.printf("Prima ");
		else
			System.out.printf("Bukan Prima ");
	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s