Menentukan Bilangan Prima Dengan Range

#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;

int main(int argc, char *argv[])
{
long bil, prima, a, b, c, d; // mendeklarasikan variabel yang di perlukan dalam program
#define true    1 // mendefinisikan bahwa angka 1 bernilai benar
#define false   0 // mendefinisikan bahwa angka 1 bernilai benar

cout << “PROGRAM BILANGAN PRIMA\n”;
cout << “============================\n\n”;
cout << “Masukkan banyak baris angka: “;
cin >> bil; // memasukkan bilangan
cout << “Hasil :\n”;
//proses
b = 1;
a = 0;
while (a < bil) {
prima = false;
while (!prima) {
b++;
d = 0;
for (c = 1; c <= sqrt(b); c++) {
if ((b%c) == 0)
d++;
}
if (d == 1)
prima = true;
}
if (prima == true) {
cout << b;
cout << ” “;
a++;
}
}
getch();

system(“PAUSE”);
return EXIT_SUCCESS; // menutup program
}

Hasilnya Adalah :

Mislakan Rangenya adalah 10. maka akan mencetak angka prima sebanyak 10 buah.

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

%d bloggers like this: