Contoh program C++

  • program luas persegi panjang dengan menggunakan fungsi

#include <iostream.h>
int persegi(int p,int l)
{
return(p*l);
}
int main()
{
int p,l;
cout<<“masukkan panjang: “;
cin>>p;
cout<<“masukkan lebar: “;
cin>>l;
cout<<“luas persegi: “<<persegi(p,l);
return 0;
}

  • Program luas persegi panjang dengan menggunakan class

#include <iostream.h>
class p_panjang {
public:
int p,l;
int luas()
{return (p*l);}
};
main() {
p_panjang a;
cout<<“masukkan p: “;
cin>>a.p;
cout<<“masukkan l: “;
cin>>a.l;
cout<<“Luas = “<<a.luas();
}

  • Contoh program mencari nilai rata-rata

#include <iostream.h>
int main()
{
int nilai1,nilai2;
float rata_rata;

cout<<“masukkan nilai tori: ” ;
cin >> nilai1;
cout<<“massukkan nilai praktek: ” ;
cin >>nilai2;
rata_rata= (nilai1+nilai2)/2;
cout << “Nilai rata-rata = ” << rata_rata << endl;
return 0;
}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s