Minggu, 10 April 2011

Program C++ untuk mencari Pembagi terbesar dari dua bilangan integer


Algoritma :
Deklarasi
       a,b,tampung : integer
Deskripsi
       While ( v != 0 )
           Tampung = a % b ;
           a = b ;
           b = tampung ;
       then
              write(a)
       endwhile

#include <iostream.h>
#include <stdio.h>

void fpb(int, int);
main()
{
   fpb(30, 18);
   fpb(45, 15);
   fpb(60, 30);
}

void fpb(int a, int b)
{
   int tampung;
   cout<<"FPB "<< a <<"dan"<< b <<" adalah : "<<endl;

   while(b != 0)
   {
     tampung = a % b;
     a = b;
     b = tampung;
   }
   cout<<"hasilnya :"<< a <<endl;
}

Program class nya :

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

class fpb {
friend istream& operator >> (istream&, fpb&);
friend ostream& operator << (ostream&, const fpb&);

public:
fpb();
void hitung_fpbnya(){
    tampung = a % b;
     //a = b;
     //b = tampung;
};

private:
    int a,b;
    int tampung;
};

fpb::fpb(){
cout << "Program mencari FPB \n" << endl;
}
istream& operator >>(istream& cin, fpb& masukan){
   cout << "Masukkan bilangan pertama : ";   cin >> masukan.a;
   cout << "Masukkan bilangan kedua : ";   cin >> masukan.b;
   cout<<endl;

return cin;
}
ostream& operator << (ostream& out, const fpb& keluaran)
{
  out<< " bilangan pertama : " <<keluaran.a<<endl;
  out<< " bilangan kedua : " <<keluaran.b<<endl;
  out<< " FPB "<< keluaran.a << " dan " << keluaran.b <<" adalah : "<<endl;
  out<< " Hasilnya = "<<keluaran.tampung<<endl;

return out;
}

void main(){
  fpb bilangan;
  cin>>bilangan;
  bilangan.hitung_fpbnya();
  cout<<bilangan;

  getch();

}

Explanation:
The program above is used to find the GCF or greatest divisor of two integers. In this program using for loop in which by using the variables a, b and tampung. The process of the program is to initialize a variable that is used after it is put through the loop that has been defined above. And produce output gcd of two integers.

1 komentar:

Unknown mengatakan...

maaf sebelumnya, kok codingnya tak coba langsung ke softwarenya ga bisa di run maupun di compile, saat aku klik execute terus klik runa malah keluarnya source file not compiled. Mohon untuk itu tolong bantuannya karena saya mau referensi untuk tugas kuliah pemrograman. Makasih :)

Posting Komentar

Template by:

Free Blog Templates