Program Prima{
faktor bilangan bulat N, tampilkan TRUE jika Prima FALSE jika bukan.
Deklarasi :
n : int
Prima : boolean
i : int
fak : int
Deskripsi :
for i <-- 2 to n do
if n mod i = 0 then
fak <-- fak+1
endif
endfor
if fak = 2 then
Prima <-- TRUE
else
Prima <-- FALSE
write (Prima)
Program
#include <cstdlib>
#include <iostream>
using namespace std;
bool Prima(int n){
int i, fak;
bool prima;
fak=1;
for ( i = 2; i <= n; i++){
if ( n % i == 0 ){
fak = fak + 1;
}
}
if ( fak == 2 ){
prima = true;
}else{
prima = false;
}
return prima;
}
int main(int argc, char *argv[])
{
int x;
cout << "Masukkan bilangan : ";
cin >> x;
if ( Prima(x) == 0 ){
cout << "Bilangan " << x << " bukan bilangan prima" <<endl;
}
else{
cout << "Bilangan " << x << " adalah bilangan prima" <<endl;
}
system("pause");
return 0;
}
Running
Geen opmerkings nie:
Plaas 'n opmerking