Senin, 27 November 2017

Published November 27, 2017 by with 0 comment

Contoh Program dalam Bahasa C++

=> Contoh Program C++ (Menghitung Keliling Persegi Panjang)

#include <iostream>
using namespace std;
int main(){
    int p, l, keliling;

    cout<<"Masukan Panjang Persegi Panjang: ";
    cin>>p;
    cout<<"Masukan Lebar Persegi Panjang: ";
    cin>>l;

    keliling=p+l+p+l;
    cout<<"Keliling Persegi Panjang adalah "<<keliling;
    return 0;
}
=> Contoh Program C++ (Penjumlahan dan Perkalian)

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

int tambah (int a,int b) {
  int x;
  x=a+b;
  return x;
}

int kali (int a,int b){
  int y;
  for (int i=1; i<=b; i++) {
  y=y+a;
}
  return y;
}
void main (){
  int a,b,x,y;

  cout<<"Nilai a: ";
  cin>>a;
  cout<<"Nilai b: ";
  cin>>b;
  x=tambah(a,b);
  cout<<"Hasil Tambah: "<<x<<endl;
  y=kali(a,b);
  cout<<"Hasil Kali: "<<y<<endl;

getch ();  
}
=> Contoh Program C++ (Sewa Mobil)

#include <iostream>
using namespace std;
int main() {
    awal:
    int l_sewa;
    char nama [30];
    char pil,yt;
    cout << "                                                                                                        " << endl;
    cout << "                         RENTAL MOBIL NITTO 25                                "<< endl;
    cout << "                                                                                                        " << endl;
    cout << "------------------------------------------------------------------------------" << endl;
    cout << "KODE        Jenis Mobil                      Harga Sewa/Hari                 " << endl;
    cout << " 1.         Sedan                           Rp. 550.000                      " << endl;
    cout << " 2.         Mini Bus                     Rp. 600.000                      " << endl;
    cout << " 3.         Bus                              Rp. 800.000                      " << endl;
    cout << "                                                                                                        " << endl;
    cout << "------------------------------------------------------------------------------" << endl;
    cout << "                                                                                                        " << endl;
    cout << "Nama                       : " ;cin>>nama;
    cout << "Kode Mobil (1/2/3) : " ;cin>>pil;
    cout << "Lama Sewa             : " ;cin>>l_sewa;
    system("cls");
   
  if (pil=='1') {
    int h_sewa,sub_tot,diskon,tot,bay,balik;
    h_sewa=550000;
    sub_tot=l_sewa*h_sewa;
        if (l_sewa>=14) (diskon=sub_tot*0.3);
        else (diskon=sub_tot*0);
    tot=sub_tot-diskon;
    cout << "Nama Penyewa          : " <<nama<< endl;
    cout << "Kode Mobil (1/2/3)   : " <<pil<< endl;
    cout << "Jenis Mobil               : Sedan" << endl;
    cout << "Harga Sewa/Hari      : " <<h_sewa<< endl;
    cout << "Lama peminjaman    : " <<l_sewa<<" hari"<< endl;
    cout << "Sub Total                  : Rp " <<sub_tot<<",-"<< endl;
    cout << "Diskon                      : Rp " <<diskon<<",-"<< endl;
    cout << "Total Bayar.              : Rp " <<tot<<",-"<< endl;
    cout<<"------------------------------------------------------------------";
    cout<<endl;
    cout<<"Uang Bayar   = Rp. ";cin>>bay;
    balik=bay-tot;
    cout<<"Uang Kembali = Rp. " <<balik<<",-"<< endl;
    }
    else if (pil=='2') {
    int h_sewa,sub_tot,diskon,tot,bay,balik;
    h_sewa=600000;
    sub_tot=l_sewa*h_sewa;
        if (l_sewa>=14) (diskon=sub_tot*0.3);
        else (diskon=sub_tot*0);
    tot=sub_tot-diskon;
    cout << "Nama Penyewa         : " <<nama<< endl;
    cout << "Kode Mobil (1/2/3)   : " <<pil<< endl;
    cout << "Jenis Mobil               : Mini Bus" << endl;
    cout << "Harga Sewa/Hari      : " <<h_sewa<< endl;
    cout << "Lama peminjaman   : " <<l_sewa<<" hari"<< endl;
    cout << "Sub Total                  : Rp " <<sub_tot<<",-"<< endl;
    cout << "Diskon                      : Rp " <<diskon<<",-"<< endl;
    cout << "Total Bayar               : Rp " <<tot<<",-"<< endl;
    cout<<"------------------------------------------------------------------";
    cout<<endl;
    cout<<"Uang Bayar   = Rp. ";cin>>bay;
    balik=bay-tot;
    cout<<"Uang Kembali = Rp. " <<balik<<",-"<< endl;
    }
    else if (pil=='3') {
    int h_sewa,sub_tot,diskon,tot,bay,balik;
    h_sewa=800000;
    sub_tot=l_sewa*h_sewa;
        if (l_sewa>=14) (diskon=sub_tot*0.3);
        else (diskon=sub_tot*0);
    tot=sub_tot-diskon;
    cout << "Nama Penyewa         : " <<nama<< endl;
    cout << "Kode Mobil (1/2/3)   : " <<pil<< endl;
    cout << "Jenis Mobil               : Bus" << endl;
    cout << "Harga Sewa/Hari      : " <<h_sewa<< endl;
    cout << "Lama peminjaman    : " <<l_sewa<<" hari"<< endl;
    cout << "Sub Total                   : Rp " <<sub_tot<<",-"<< endl;
    cout << "Diskon                       : Rp " <<diskon<<",-"<< endl;
    cout << "Total Bayar                : Rp " <<tot<<",-"<< endl;
    cout<<"------------------------------------------------------------------";
    cout<<endl;
    cout<<"Uang Bayar   = Rp. ";cin>>bay;
    balik=bay-tot;
    cout<<"Uang Kembali = Rp. " <<balik<<",-"<< endl;
    }
    else {
    cout<<"Tidak Bisa Dipersoses";
    cout<<endl;
    }
    cout<<endl;
    cout<<"Apakah anda ingin mengulang ? [Y/T] : ";
    cin>>yt;
    cout<<endl;

    if(yt=='Y' || yt=='y')
    {goto awal;}
    if(yt=='T' || yt=='t')
    {goto selesai;}
    selesai:
   
  return 0;

}

0 komentar:

Posting Komentar

Diberdayakan oleh Blogger.