Lompat ke konten Lompat ke sidebar Lompat ke footer

Contoh Program C++



Standar Kopetensi  hari Pertama
+variabel C++
+Matematika Dasar pada C++
+penggunaan perintah Input dan Output
+Penggunaan char / String C++

+Pembuatan Fungsi pada C++


Latihan 1:

=========================================================================================
#include <iostream.h>
int main (){
int pilih;   // deklarasi variabel
float bil1,bil2,hasil;
cout<<"####################################"<<endl;    
cout<<"##  APLIKASI KALKULATOR LP2M-ARAY ##"<<endl;    
cout<<"##              HARI I            ##"<<endl;    
cout<<"####################################"<<endl;    
cout<<endl;

cout<<"Masukkan Bil1 :";
cin>>bil1;
cout<<"Masukkan Bil2 :";
cin>>bil2;
hasil=bil1+bil2;
cout<<endl;
cout<<"Hasil="<<hasil;
     cin.get();
  return 0;
}

=========================================================================================

Latihan 2

==========================================================================

#include <iostream.h>
#include <string.h>

int pangkat(int a, int b){
int hasil,i;
hasil=1;
for(i=1;i<=b;i++){
hasil= hasil * a;
}
return hasil;
}


int main (){
    char *operators= "???";
int pilih;
float bil1,bil2,hasil;
cout<<"####################################"<<endl;   
cout<<"##  APLIKASI KALKULATOR LP2M-ARAY ##"<<endl;   
cout<<"##         HARI I-LAT 2           ##"<<endl;   
cout<<"####################################"<<endl;   
cout<<endl;

cout<<"+++++++++++++++++++++++++++++++"<<endl;    
cout<<"Masukkan Bil1 :";
cin>>bil1;
cout<<"Masukkan Bil2 :";
cin>>bil2;

cout<<"PILIH OPERATOR ANDA"<<endl;   
cout<<"1)Penjumlahan"<<endl;   
cout<<"2)Pengurangan"<<endl;   
cout<<"3)Perkalian"<<endl;   
cout<<"4)Pembagian"<<endl;   
cout<<"5)Modulo"<<endl;   
cout<<"6)Pangkat"<<endl;   
cout<<"?)None"<<endl;   
cout<<endl;
cout<<"+++++++++++++++++++++++++++++++"<<endl;   

cout<<"Pilihlah Operator Anda :";
cin>>pilih;


if (pilih==1){hasil=bil1+bil2;operators="Penjumlahan";}
else if (pilih==2){hasil=bil1-bil2;operators="Pengurangan";}
else if (pilih==3){hasil=bil1*bil2;operators="Perkalian";}
else if (pilih==4){hasil=bil1/bil2;operators="Pembagian";}
else if (pilih==5){hasil=(int) bil1 % (int) bil2;operators="Modulo";}
else if (pilih==6){hasil=pangkat(bil1,bil2);operators="Pangkat";}
else {hasil=0;operators="None";}

cout<<endl<<"Anda memilih proses "<<operators<<endl;
cout<<"Hasil="<<hasil;
  
  return 0;
}


==========================================================================================

Latihan 3

==========================================================================================
#include <iostream.h>
#include <string.h>

int pangkat(int a, int b){
int hasil,i;
hasil=1;
for(i=1;i<=b;i++){
hasil= hasil * a;
}
return hasil;
}


int awal (){
     cout<<"####################################"<<endl;    
cout<<"##  APLIKASI KALKULATOR LP2M-ARAY ##"<<endl;    
cout<<"##         HARI I-LAT 3           ##"<<endl;    
cout<<"####################################"<<endl;    
cout<<endl;
return 0;
}

int input(){
char *operators= "???";
int pilih;
float bil1,bil2,hasil;

cout<<"+++++++++++++++++++++++++++++++"<<endl;    
cout<<"Masukkan Bil1 :";
cin>>bil1;
cout<<"Masukkan Bil2 :";
cin>>bil2;

cout<<"PILIH OPERATOR ANDA"<<endl;    
cout<<"1)Penjumlahan"<<endl;    
cout<<"2)Pengurangan"<<endl;    
cout<<"3)Perkalian"<<endl;    
cout<<"4)Pembagian"<<endl;    
cout<<"5)Modulo"<<endl;    
cout<<"6)Pangkat"<<endl;   
cout<<endl;
cout<<"+++++++++++++++++++++++++++++++"<<endl;    

cout<<"Pilihlah Operator Anda :";
cin>>pilih;

if (pilih==1){hasil=bil1+bil2;operators="Penjumlahan";}
else if (pilih==2){hasil=bil1-bil2;operators="Pengurangan";}
else if (pilih==3){hasil=bil1*bil2;operators="Perkalian";}
else if (pilih==4){hasil=bil1/bil2;operators="Pembagian";}
else if (pilih==5){hasil=(int) bil1 % (int) bil2;operators="Modulo";}
else if (pilih==6){hasil=pangkat(bil1,bil2);operators="Pangkat";}

cout<<endl<<"Anda memilih proses "<<operators<<endl;
cout<<"Hasil="<<hasil;
   //=======================
cout<<endl;
cout<<"+++++++++++++++++++++++++++++++"<<endl;    
cout<<"Selanjutnya"<<endl;    
cout<<"1)Lanjutkan"<<endl;
cout<<"2)Berhenti"<<endl;
cout<<endl;
cout<<"Pilihlah Keputusan Anda :";
cin>>pilih;
cout<<"+++++++++++++++++++++++++++++++"<<endl;    

if (pilih==1){input();}
else if (pilih==2){
cout<<"T A M A T"<<endl; 
}
  return 0;
}

int main(){
awal();
input();
  return 0;
}

Posting Komentar untuk "Contoh Program C++"