Pages

Saturday 24 December 2016

Problem 60 - Display first n prime number

Display first n primary number adalah suatu cara untuk menampilkan berapa banyak bilangan prima dari awal di mulai dari 2 
Code di bawah ini

DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1. import java.util.Scanner;
  2. class PrimeNumberDemo
  3. {
  4.    public static void main(String args[])
  5.    {
  6.       int n;
  7.       int status = 1;
  8.       int num = 3;
  9.       Scanner scanner = new Scanner(System.in);
  10.       System.out.println("Berapa bilangan prima : ");
  11.       n = scanner.nextInt();
  12.       if (n >= 1)
  13.       {
  14.          System.out.println(n+ "Urutan Bilangan Prima awal");
  15.          System.out.println(2);
  16.       }
  17.       for ( int i = 2 ; i <=n ;  )
  18.       {
  19.          for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
  20.          {
  21.             if ( num%j == 0 )
  22.             {
  23.                status = 0;
  24.                break;
  25.             }
  26.          }
  27.          if ( status != 0 )
  28.          {
  29.             System.out.println(num);
  30.             i++;
  31.          }
  32.          status = 1;
  33.          num++;
  34.       }        
  35.    }
  36. }



Disini saya input 15

0 comments:

Post a Comment