Program Menghitung KPK dan FPB

Program menghitung KPK dan FPB dua bilangan yang di iinputkan. Berikut programnya :

#include <cstdlib>
#include <iostream>

using namespace std;

// mendeklarasikan fungsi FPB
int FPB(int m,int n){
int r,FPB;
while(n!=0){
r=m % n;
m=n;
n=r;
}
FPB=m;
return FPB;
}
// mendeklarasikan fungsi KPK
int KPK(int n, int m){
int x;
x=(n*m)/FPB(n,m);
return x;
}

int main(int argc, char *argv[])
{
int pil;
do{
cout<<“Program Mencetak hasil FPB dan KPK 2 Bilangan :”<<endl;
cout<<“Menu pilihan\n”;
cout<<“1. FPB dan KPK\n “;
cout<<“0.Keluar program\n”;
cout<<“pilihan anda (1 dan 0)\n”;
cin>>pil;
switch(pil){
case 1:{
cout<<“Masukkan bilangan 1 : “;int bil1;cin>>bil1;
cout<<“Masukkan bilangan 2 : “;int bil2;cin>>bil2;
cout<<“FPB dari “<<bil1<<” dan “<<bil2<<” : “<<FPB(bil1,bil2)<<endl;
cout<<“KPK dari “<<bil1<<” dan “<<bil2<<” : “<<KPK(bil1,bil2)<<endl;
}; break;

}cout<<endl;
}while(pil!=0);

system(“PAUSE”);
return EXIT_SUCCESS;
}

Hasil programnya :

#include<iostream>
#include<conio>
#include<math.h>

void Maxlist(int n);
void PosisiMaxlist(int n);
int isPrime(int p);
void factorbil(int n);
void faktorprim(int n);
int FPB (int m,int n);
int KPK (int n, int m );
int NumPrimeth(int n);

void main(){
int pil;
do{
cout<<“Menu pilihan\n”;
cout<<“1. Cari bilangan terbesar pada list\n”;
cout<<“2. Cari bilangan terbesar dan posisisnya pada pada list\n”;
cout<<“3. Tes Prima\n”;
cout<<“4. Faktor Bilangan\n”;
cout<<“5. Faktor Prima\n”;
cout<<“6. FPB dan KPK\n “;
cout<<“0.Keluar program\n”;
cout<<“pilihan anda (0..6)\n”;
cin>>pil;
clrscr();
switch(pil){
case 1:{
cout<<“banyak data : “;int bil;cin>>bil;Maxlist(bil);
};break;
case 2:{
cout<<“banyak data : “;int bil;cin>>bil;PosisiMaxlist(bil);
};break;
case 3:{
cout<<“Masukkan bilangan bulat : “;int bil;cin>>bil;
if(isPrime(bil))cout<<bil<<“adalah PRIMA”;
else cout<<bil<<“BUKAN PRIMA”;
};break;
case 4:{
cout<<“Masukkan bilangan bulat : “;int bil;cin>>bil;
cout<<“Faktor dari “<<bil<<” adalah “;
factorbil(bil);
};break;
case 5:{
cout<<“Masukkan bilangan bulat : “;int bil;cin>>bil;
cout<<“Faktor prima dari “<<bil<<” adalah “;
faktorprim(bil);
};break;
case 6:{
cout<<“Masukkan bilangan 1 : “;int bil1;cin>>bil1;
cout<<“Masukkan bilangan 2 : “;int bil2;cin>>bil2;
cout<<“FPB dari “<<bil1<<” dan “<<bil2<<” : “<<FPB(bil1,bil2)<<endl;
cout<<“KPK dari “<<bil1<<” dan “<<bil2<<” : “<<KPK(bil1,bil2)<<endl;
}; break;

}cout<<endl;getch();
}while(pil!=0);
}

void Maxlist(int n){
int max=-10000;
for(int i=1;i<=n;1+1){
cout<<“bil ke “<<i<<” : “;
int bil[100];cin>>bil[100];
if(bil[i]>max) max=bil[i];
}
cout<<“max = “<<max;
}

void PosisiMaxlist(int n){
int pos,max=-10000;
for(int i=1;i<=n-1;i+=1){
cout<<“bil ke “<<i<<” : “;
int bil[100];cin>>bil[100];
if(bil[i]>max) {max=bil[i];pos=i+1;}
};
cout<<“max = “<<max<<” pada posisi “<<pos;
}

int isPrime(int p){
float sqrtp = sqrt(p);
if(p<2) return 0;
if(p==2)return 1;
if(p%2==0) return 0;
for(int d=3; d<=sqrtp;d+=2)
if(p%d==0) return 0;
return 1;
}

void factorbil(int n){
int k = 0 ;int fakbil[100];
for(int i=1;i<=n;i+=1){
if(n%i==0){
k=k+1;
fakbil[k]=i;
}
};
for (int j=1;j<=k;j+=1) cout<<” “<<fakbil[j];
}

int FPB(int m,int n){
int r,FPB;
while(n!=0){
r=m % n;
m=n;
n=r;
}
FPB=m;
return FPB;
}

void faktorprim(int n){
for(int x=1;x<=n;x++){
if(n%x==0){
if(x<=10){
if((x==2)||(x==3)||(x==5)||(x==7))
cout<<x<<“,”;}
else if(x>10){
if((x%2!=0)&&(x%3!=0)&&(x%5!=0)&&(x%7!=0))
cout<<x;}
}
}
}

int KPK(int n, int m){
int x;
x=(n*m)/FPB(n,m);
return x;
}

int NumPrimeth(int n){
int m;
for(int x=0;x<=n;x++){
if(x<=10){
if((x==2)||(x==3)||(x==5)||(x==7))
m=x;}
else if(x>10){
if((x%2!=0)&&(x%3!=0)&&(x%5!=0)&&(x%7!=0))
m=x;}
}
return m;
}

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: