Jumat, 25 Maret 2011

Belajar program C++


1.  Pada kali ini saya mempunyai sebuah kasus bagaimana membuat program untuk mengkonversi jam menjadi menit.
Berikut kode programnya :
#include<iostream.h>
#include<conio.h>

int main()
{
awal:
  char jawab;
  int jam;
  cout << "Mengkonversi jam menjadi menit" <<endl;
  cout << "Masukan jamnya = "; cin >> jam;
  cout << jam << " jam adalah " << jam * 60 << " menit ";
  cout << "\n";
  cout << "\n apakah anda ingin menghitung lagi ?\n";
  cout << "Y = Setuju\n";
  cout << "T = Tidak Setuju\n";
  cout << "=> ";
  cin >> jawab;
  if ( jawab == 'y' || jawab == 'Y' )
    goto awal;
  else
    clrscr();
  cout << "makasiih... ";

  getch();
  return 0;
}

In this program we first have to declare a variable-variable that will be used, and also the type of data, in this case the data type that I use is a interger and char, and a variable that is in use, among others, responsibility and hours. In this program I also use the election if ... else where used for the program if we choose to count again.



2.  In this case, learn how to make a square of stars in the C + +. There are some terms here that I should give the square width must be between 1 and 20. Here I use to print a statement for the star. Then in the for statement, we add another if statement, use in order to determine the index to just how many stars will be printed.
Berikut kode programnya :
#include <iostream.h>
#include <conio.h>

int main()
{
  int ukuran;
  do
  {
    cout<<"Masukkan ukuran persegi: ";
    cin>>ukuran;
  }while(ukuran<1||ukuran>20);

  cout<<endl;

  for(int i=1; i<= ukuran; i++)
  {
     for(int j=1; j<=ukuran; j++)
     {
       if(i==1||j==1||i==ukuran||j==ukuran)
     {
       cout<<"*";
     }
      else
     {
      cout<<" ";
     }
  }
  cout<<endl;
 }

cout<<"begitu lah model persegi panjangnya "<<endl;
cout<<"terima kasih"<<endl;

 getch();
 return 0;
}


3.  Di sini saya akan membahas kasus tentang pemilihan. Di mana kita dapat memilih dari beberapa pilihan yang muncul pada program. Kemudian dengan otomatis program akan menampilkan pilihan yang kita pilih tadi.
Berikut kode programnya :

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

void main()
{
 int pilihan;
 clrscr();
 cout<<"        CONTOH MENU PILIHAN    "<<endl<<endl;
 cout<<"================================="<<endl;
 cout<<"| 1. Pilihan Pertama   |"<<"soto    |"<<endl;
 cout<<"| 2. Pilihan Kedua     |"<<"mi ayam |"<<endl;
 cout<<"| 3. Pilihan Ketiga    |"<<"bakso   |"<<endl;
 cout<<"| 4. Pilihan Keempat   |"<<"rawon   |"<<endl;
 cout<<"================================="<<endl;
 cout<<"Pilihan Anda (1/2/3/4): ";
 cin>>pilihan;

 cout<<"\n\n";
 if(pilihan==1)
 {cout<<"Anda Memilih Menu Satu = soto";}
 if(pilihan==2)
 {cout<<"Anda Memilih Menu Dua = mi ayam";}
 if(pilihan==3)
 {cout<<"Anda Memilih Menu Tiga = bakso";}
 if(pilihan==4)
 {cout<<"Anda Memilih Menu Empat = rawon";}
 cout<<endl;
 cout<<"selamat menikmati.... "<<endl;
 getch();
}

0 komentar:

Posting Komentar

Template by:

Free Blog Templates