Random Prime Numbers 0-100
Source Code:
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
/**
*bundet.com
*C++ Matrik Bilangan Prima Acak 0-100
*/
void main(){
int nilai[100],x;
randomize();
cout << "100 angka yang diinputkan, dan bilangan prima yang diperoleh secara acak : " <<endl<<endl;
for (int i=0; i<100; i++){
nilai[i]=random(100);
x=0;
for (int j=2; j<=nilai [i]; j++){
if (nilai[i]%j==0){
x++;
}
}
if (x==1 && nilai [i]!=0){
cout <<nilai[i]<<" ";
}
}
getch();
}
Prime Matrix Once for Prime Multiple
Source Code:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
/**
*bundet.com
*Matrik Menampilkan Sekali Bilangan Prima Multiple
*/
void main() {
int nilai[100], x, y=0, z, prima[100];
randomize();
cout << "100 angka yang diinputkan secara acak: " << endl;
for (int i=0; i<100; i++) {
nilai[i]=random(100);
cout << nilai[i] << " ";
}
cout << endl << endl << "Bilangan Prima dari hasil inputan diatas: " << endl;
for (int i=0; i<100; i++) {
x=0;
for (int j=2; j<=nilai[i]; j++) {
if (nilai[i]%j==0) {
x++;
}
}
if (x==1 && nilai[i]!=0) {
prima[y]=nilai[i];
cout << prima[y] << " ";
y++;
}
}
cout << endl << endl << "Bilangan prima diatas yg sama dimunculkan sekali: " << endl;
for (int j=0; j<y; j++) {
if (j==0) {
cout << prima[0] << " ";
}
else {
z=0;
for (int k=0; k<j; k++) {
if (prima[j] == prima[k]) {
z++;
}
}
if (z==0) {
cout << prima[j] << " ";
}
}
}
getch();
}
Hope this is useful & happy learning!