Java : Program Pencari Bilangan Prima dengan Batas-Batas Tertentu

Last Updated on 13 years by Hafizh Herdi

Oke sebenarnya ini adalah salah program jadul sederhana buatan saya. Dimana fungsi utamanya adalah untuk mengecek jumlah bilangan prima pada batas-batas tertentu. Misalnya kita ingin mengecek ada berapa bilangan prima di antara angka 1 sampai 10, maka program ini akan mengeluarkan ouput berupa 4, karena pada angka 1 sampai sepuluh ada 4 bilangan prima. Yaitu 2, 3, 5 dan 7.

Oiya, program ini aslinya saya buat untuk teman saya. haha Dibikin dulu waktu saya sedang seneng-senengnya pake Linux. Kalo nggak salah ini dibikin menggunakan Geany IDE di Linux Ubuntu.

    1. /*
    2.  *      cekprima.java
    3.  *      
    4.  *      Copyright 2011 Hafizh Herdi Naufal <twohnuvo@gmail.com>
    5.  *      http://twitter.com/twoh | http://twoh.co
    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.  *      
  1.  *      You should have received a copy of the GNU General Public License
  2.  *      along with this program; if not, write to the Free Software
  3.  *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  4.  *      MA 02110-1301, USA.
  5.  */
  6. import java.util.Scanner;
  7. class cekprima
  8. {
  9.         public static boolean cekpr(int i)
  10.         {
  11.             boolean k = true;
  12.             int j;
  13.             for(j = 2; j <= i; j++)
  14.             {
  15.                 if(i % j == 0 && j != i)
  16.                 {
  17.                     k = false;
  18.                 }
  19.             }
  20.             return k;
  21.         }
  22.         public static void main(String[] TWOHSyndicate)
  23.         {
  24.             Scanner s = new Scanner(System.in);
  25.             int awal, akhir;
  26.             System.out.println(“Masukkan batas awal bilangan prima : “);
  27.             awal = s.nextInt();
  28.             System.out.println(“Masukkan batas akhir bilangan prima : “);
  29.             akhir = s.nextInt();
  30.             boolean pr = false;
  31.             for(int i = awal; i <= akhir; i++)
  32.             {
  33.                 pr = cekpr(i);
  34.                 if (pr == true)
  35.                 {
  36.                     if(i != 1)
  37.                     {
  38.                         System.out.print(” “+i);
  39.                     }
  40.                 }
  41.             }
  42.         }
  43. }

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





Download aplikasi kami di Google Play Store


Tutorial Menarik Lainnya :

Leave a Reply

Your email address will not be published. Required fields are marked *

TWOH&Co.