Java : Program Pencari BIlangan Prima dengan Batas-Batas Tertentu


  1.  
  2. /*
  3.  *      cekprima.java
  4.  *      
  5.  *      Copyright 2011 Hafizh Herdi Naufal <twohnuvo@gmail.com>
  6.  *      This program is free software; you can redistribute it and/or modify
  7.  *      it under the terms of the GNU General Public License as published by
  8.  *      the Free Software Foundation; either version 2 of the License, or
  9.  *      (at your option) any later version.
  10.  *      
  11.  *      This program is distributed in the hope that it will be useful,
  12.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *      GNU General Public License for more details.
  15.  *      
  16.  *      You should have received a copy of the GNU General Public License
  17.  *      along with this program; if not, write to the Free Software
  18.  *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19.  *      MA 02110-1301, USA.
  20.  */
  21.  
  22. import java.util.Scanner;
  23.  
  24. class cekprima
  25. {
  26.         public static boolean cekpr(int i)
  27.         {
  28.             boolean k = true;
  29.             int j;
  30.             for(j = 2; j <= i; j++)
  31.             {
  32.                 if(i % j == 0 && j != i)
  33.                 {
  34.                     k = false; 
  35.                 }
  36.             }
  37.             return k;
  38.         }
  39.    
  40.         public static void main(String[] TWOHSyndicate)
  41.         {
  42.             Scanner s = new Scanner(System.in);
  43.             int awal, akhir;
  44.            
  45.             System.out.println("Masukkan batas awal bilangan prima : ");
  46.             awal = s.nextInt();
  47.             System.out.println("Masukkan batas akhir bilangan prima : ");
  48.             akhir = s.nextInt();
  49.             boolean pr = false;
  50.             for(int i = awal; i <= akhir; i++)
  51.             {          
  52.                 pr = cekpr(i);
  53.                 if (pr == true)
  54.                 {
  55.                     if(i != 1)
  56.                     {
  57.                         System.out.print(" "+i);
  58.                     }
  59.                 }
  60.             }
  61.         }
  62. }
  63.  

Enjoy!
Inilah program pertama yang aku buat di bahasa Java~ Hope it will do good for me.

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