Rabu, 13 April 2011

contoh program while do c++

#include <iostream.h>
int main()
{
int i,n,jum,angka;
char pil;
pil='y';
while ((pil=='y') || (pil=='Y'))
{
cout<<"input banyak perulangan : ";cin>>n;
jum=0;
i=1;
while (i<=n)
{
cout<<"perulangan ke-"<<i<<":";cin>>angka;
jum=jum+angka;
i++;
}
cout<<"hasil penjumlahan : "<<jum<<endl;
cout<<"apakah anda mau mengulang (y/t) : ";cin>>pil;
}
return 0;
}

contoh program while do c++

#include <iostream.h>
int main ()
{
int i;
i=1;
while (i<=10)
{
cout<<i<<endl;
i=i + 1;
}
return 0;
}

contoh program struct c++ luas lingkaran

#include<iostream.h>
int main()
{

int r;
double luas;

cout<<" PROGRAM HITUNG LUAS LINGKARAN "<<endl;
cout<<endl;
cout<<"input jari-jari lingkaran : ";
cin>>r;
luas = 3.14*r*r;
cout<<"Luas lingkaran adalah : "<<luas<<endl;

return 0;
}

contoh program c++ input kode & harga barang

#include<iostream.h>
int main()
{
char kode;
char* nama_barang;
char* harga_barang;
long int tot_harga, jumlah;

cout<<" DAFTAR NAMA BARANG BERDASARKAN KODE \n";
cout<<" *********************************** \n";
cout<<" 1: monitor, harga Rp 500000 \n";
cout<<" 2: cpu harga Rp 1000000\n";
cout<<" 3: hardisk harga Rp 400000\n";
cout<<" *********************************** \n";
cout<<"\n";

cout<<"input kode barang : ";
cin>>kode;
cout<<"input jumlah barang : ";
cin>>jumlah;

if (kode =='1' )
{
nama_barang ="monitor";
harga_barang ="500000";
tot_harga=jumlah*500000;
}

if (kode =='2' )
{
nama_barang ="cpu";
harga_barang ="1000000";
tot_harga=jumlah*1000000;
}

if (kode =='3' )
{
nama_barang ="hardisk";
harga_barang ="400000";
tot_harga=jumlah*400000;
}

cout<<"nama barang "  <<nama_barang<<endl;
cout<<"harga barang "  <<harga_barang<<endl;
cout<<"Total harga barang Rp " <<tot_harga<<endl;

return 0;
}

contoh program c++ menghitung rumus matematika

#include <iostream.h>
#include <cstdlib>
int main ()
{
int bil;
double L,a,t;
double p,l;
double r;

cout<<"PROGRAM HITUNG RUMUS MATEMATIKA \n";
cout<<"******************************* \n";

    atas :cout<<"1 :Luas segitiga \n";
     cout<<"2 :Luas persegi panjang \n";
     cout<<"3 :Luas lingkaran \n";
     cout<<"4 :Exit \n";
            
cout<<"******************************* \n";

 cout<<"input case :";
 cin>>bil;

 switch (bil)
 {
 case 1 :cout<<"input alas :";
     cin>>a;
 cout<<"input tinggi :";
 cin>>t;
                  L=a*t/2;
                  cout<<"luas segitiga :"<<L<<endl;
 goto atas;
 break;
 case 2 :cout<<"input panjang :";
     cin>>p;
 cout<<"input lebar :";
 cin>>l;
 L=p*l;
 cout<<"luas persegi panjang :"<<L<<endl;
 goto atas;
 break;
 case 3 :cout<<"input jari-jari :";
     cin>>r;
 L=2*3.14*r;
 cout<<"luas lingkaran :"<<L<<endl;
 goto atas;
 break;
 case 4 :cout<<"exit \n";
     break;
 }
 return 0;
}

contoh program c++ grade nilai

#include <iostream.h>
int main ()
{
char mtkul[20];
char sks,nh;
double kehadiran,tm,uts,uas,na;

cout<<"input mata kuliah        :";
cin>>mtkul;

cout<<"input jumlah sks         :";
cin>>sks;

cout<<"input kehadiran          :";
cin>>kehadiran;

cout<<"input nilai tugas mandiri:";
cin>>tm;

cout<<"input nilai uts          :";
cin>>uts;

cout<<"input nilai uas          :";
cin>>uas;

na=(kehadiran*0.10)+(tm*0.15)+(uts*0.25)+(uas*0.50);
cout<<"nilai akhir                  :"<<na<<endl;
if (na>=80 && na<=100)
{
nh= 'A';
}

else if (na>=70 && na<=79)
{
    nh= 'B';
}

else if (na>=60 && na<=69)
{
    nh= 'C';
}

else if (na>=50 && na<=59)
{
    nh= 'D';
}

else if (na<=49)
{
    nh= 'E';
}

else
{
cout<<"Anda salah input nilai "<<endl;
}
    cout<<"*************************"<<endl;
    cout<<"mata kuliah  :"<<mtkul<<endl;
cout<<"jumlah sks   :"<<sks<<endl;
cout<<"nilai akhir  :"<<na<<endl;
cout<<"nilai huruf  :"<<nh<<endl;
cout<<"**************************"<<endl;

return 0;
}

contoh program c++ menentukan bil genap[ & bil ganjil

#include <iostream.h>
int main ()
{
int bil;
cout<<"input sebuah bilangan :";
    cin>>bil;
if (bil %2==0)
{
cout<<bil<<"bilangan genap"<<endl;
}
else
{
cout<<bil<<"bilangan ganjil"<<endl;
}
return 0;
}

contoh program fuction c++ penjumlahan

#include <iostream.h>
int main ()
{
int a,b,c;
cout<<"PROGRAM PENJUMLAHAN"<<endl;
cout<<"Input nilai a : ";
cin>>a;
cout<<"Input nilai b : ";
cin>>b;
c=a+b;
cout<<"Hasil Penjumlahan : "<<c<<endl;
return 0;
}

contoh program array c++ input data

#include <iostream.h>
int main()
{
int data[100];
int n,i;

cout<<"===PROGRAM INPUT DATA==="<<endl;
cout<<"Masukan Jumlah Data: ";
cin>>n;
cout<<endl;

for(i=1;i<=n;i++)
{
cout<<"Masukkan data ke "<<i<< " : ";
cin>>data[i];
}

cout<<"Data yang di Masukan: ";
cout<<endl;

for( i=1; i<=n; i++)
{
cout<<" "<<data[i]<<endl;
}

return 0;
}

contoh program c++ menghitung diskon 10%

#include <iostream.h>
int main ()
{
char nama_brg[20];
double hrg,diskon,total_hrg,uang,kembali;

cout<<"input nama barang :";
cin>>nama_brg;

cout<<"input harga barang :";
cin>>hrg;

if (hrg >=100000)
{
diskon=hrg*10/100;
cout<<"diskon"<<diskon<<endl;
}

else if (hrg <=100000)
{
diskon=hrg*0/100;
cout<<"diskon"<<diskon<<endl;
}

total_hrg=hrg-diskon;
cout<<"total harga :"<<total_hrg<<endl;

cout<<"input jumlah uang :";
cin>>uang;

kembali=uang-total_hrg;
cout<<"kembali :"<<kembali<<endl;
return 0;
}

contoh program c++ bulan & angka

#include <iostream.h>
#include <cstdlib>
int main ()
{
int angka;

cout<<"input angka =";
cin>>angka;

switch (angka)
{
case 2:cout<<"31 hari \n";
break;
    case 1:
case 3:
    case 5:
case 7:
case 8:
    case 10:
case 12:cout<<"31 hari \n";
break;

    case 4:
case 6:
    case 9:
case 11:cout<<"30 hari \n";
break;
}
return 0;
}

contoh program c++ menentukan bil positif & bil negatif

#include <iostream.h>
int main ()
{
int bil;
cout<<"input sebuah bilangan :";
cin>>bil;
if (bil > 0)
{
cout<<bil<<"bilangan positif"<<endl;
}
else if (bil < 0)
{
cout<<bil<<"bilangan negatif"<<endl;
}
else
cout<<bil<<"bilangan nol"<<endl;
return 0;
}

contoh program class c++ bakery

#include<iostream.h>
#include<cstdlib>
class bakery
{
public :
int kode,jum;
long int hrg,tothrg,disk,totdisk;

void tampilan_awal()
{
cout<<"***CAFE BAKERY***"<<endl;
cout<<"  1. Bolu Gulung "<<endl;
cout<<"  2. Roti Keju   "<<endl;
cout<<"  3. Mini Pizza  "<<endl;
}

void input()
{
   cout<<"Input kode Bakery  : ";cin>>kode;
switch (kode)
{
case 1:cout<<"Bakery             : Bolu Gulung "<<endl;
  hrg=3300;
  cout<<"Harga Bakery       : "<<hrg<<endl;
  cout<<"Jumlah Bakery      : ";cin>>jum;
  tothrg=hrg*jum;
  cout<<"Total Harga        : "<<tothrg<<endl;
  if(jum>4)
  {
  disk=tothrg*10/100;
      cout<<"Diskon             : "<<disk<<endl;
  totdisk=tothrg-disk;
  cout<<"Total Harga diskon : "<<totdisk<<endl;
  }
  if(jum>=10)
  {
  cout<<"GRATIS 1 BOLU GULUNG "<<endl;
  }
  break;
case 2:cout<<"Bakery             : Roti Keju "<<endl;
  hrg=4400;
  cout<<"Harga Bakery       : "<<hrg<<endl;
  cout<<"Jumlah Bakery      : ";cin>>jum;
  tothrg=hrg*jum;
  cout<<"Total Harga        : "<<tothrg<<endl;
  if(jum>4)
  {
  disk=tothrg*5/100;
  cout<<"Diskon             : "<<disk<<endl;
  totdisk=tothrg-disk;
  cout<<"Total Harga diskon : "<<totdisk<<endl;
  }
  if(jum>=10)
  {
  cout<<"GRATIS 1 ROTI KEJU "<<endl;
  }
  break;
case 3:cout<<"Bakery             : Mini Pizza "<<endl;
  hrg=5500;
  cout<<"Harga Bakery       : "<<hrg<<endl;
  cout<<"Jumlah Bakery      : ";cin>>jum;
  tothrg=hrg*jum;
  cout<<"Total Harga        : "<<tothrg<<endl;
  if(jum>4)
  {
  disk=tothrg*10/100;
      cout<<"Diskon             : "<<disk<<endl;
  totdisk=tothrg-disk;
  cout<<"Total Harga diskon : "<<totdisk<<endl;
  }
  if(jum>=10)
  {
  cout<<"GRATIS MINI PIZZA "<<endl;
  }
  break;
}
}
};

int main()
{
char ulang;
ulang='y';
while((ulang=='Y') || (ulang=='y'))
{
system("cls");

bakery cafe;
cafe.tampilan_awal();
cafe.input();
cout<<"Ingin Mengulang ? : ";cin>>ulang;
}
return 0;
}

contoh program class c++

#include <iostream.h>
class keluarga
{
public :
void orangtua()
{
cout<<"ayah membaca koran "<<endl;
cout<<"ibu memasak sayur "<<endl;

}

void anak ()
{
cout<<"anak belajar matematika "<<endl;
}

void cucu ()
{
cout<<"cucu bermain bola "<<endl;
}
};
int main()
{
keluarga kita;
kita.orangtua ();
kita.anak ();
kita.cucu ();
return 0;
}