Pages

Friday, 12 February 2016

Menampilkan Bilangan Prima antara 1 - N || Pemograman Java

       Buat program menampilkan bilangan Prima antara 1 sampai N!

P E N Y E L E S A I A N :
package SoalNo5;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        System.out.println("Program Menampilkan Bilangan Prima sebanyak n");
        System.out.println("__________________________________________________________");

        Scanner x = new Scanner (System.in);
        int i, j, n;
        System.out.print("Bilangan dimulai dari = ");
        n = x.nextInt();

        for(i=1;i<=n;i++)
        {
            int a=0;
         for(j=1;j<=n;j++)
          {
           if(i%j==0)
            {
             a++;
            }

          }
            if ((a==2)&&(n!=1))
            {
             System.out.println(i);
            }
        }
    }

}
H A S I L :

0 komentar:

Post a Comment