//* *
//* determin.cpp *
//* *
//* Contoh pemakaian operator + dan - *
//* untuk menentukan nilai dari : *
//* *
//* d = b*b - 4ac *
//* *
//* *
#include <iostream.h>
#include <conio.h>
void main ()
{
int a, b, c, d;
clrscr () ;
a = 5;
b = 600;
c = 5;
d = b * b - 4 * a * c;
Hasil Program:
Program 3.2
//* *
//* modulus.cpp *
//* *
//* Contoh untuk melihat sisa pembagian dengan *
//* menggunakan operator %. *
//* *
#include <iostream.h>
#include <conio.h>
void main ()
{
clrscr () ;
getche();
}
Hasil Program:
Program 3.3
//* *
//* priorit.cpp *
//* *
//* Contoh penggunaan tanda kurung untuk mengatur *
//* prioritas pengerjaan terhadap suatu operasi. *
//* *
#include <iostream.h>
#include <conio.h>
void main ()
{
int x;
clrscr () ;
x = 2 + 3 * 2;
cout << "x = " << x << '\n';
x = (2 + 3) * 2;
cout << "x = " << x << '\n';
getche();
}
Hasil Program:
Program 3.4
//* *
//* priorit.cpp *
//* *
//* Contoh penggunaan tanda kurung untuk mengatur *
//* prioritas pengerjaan terhadap suatu operasi. *
//* *
#include <iostream.h>
#include <conio.h>
void main ()
{
int x;
clrscr () ;
x = 2 + 3 * 2;
cout << "x = " << x << '\n';
x = (2 + 3) * 2;
cout << "x = " << x << '\n';
getche();
}
Hasil Program:
Program 3.5
//* *
//* NAIK2.C *
//* *
//* Contoh kedua pemakaian operator ++, *
//* dengan peletakan ++ di depan variabel *
//* *
#include <iostream.h>
#include <conio.h>
void main ()
{
int r = 10;
int s;
clrscr () ;
s = 10 + ++r;
cout << "r = " << r << '\n';
cout << "s = " << s << '\n';
getche();
}
Hasil Program:
Program 3.6
//* *
//* gsrkiri.cpp *
//* *
//* Contoh untuk melihat pengaruh operator << *
//* *
#include <iostream.h>
#include <conio.h>
void main ()
{
//* *
//* gsrkanan.cpp *
//* *
//* Contoh untuk melihat pengaruh operator >> *
//* *
#include <iostream.h>
#include <conio.h>
void main ()
{
unsigned int x = 93;
clrscr () ;
cout << "Nilai x semula = " << x << '\n';
x = x >> 1; // geser ke kanan 1 bit
cout << "Nilai x kini = " << x << '\n';
getche();
}
Hasil Program:
Program 3.8
//* *
//* gsrkanan.cpp *
//* *
//* Contoh untuk melihat pengaruh operator >> *
//* *
#include <iostream.h>
#include <conio.h>
void main ()
{
unsigned int x = 93;
clrscr () ;
cout << "Nilai x semula = " << x << '\n';
x = x >> 1; // geser ke kanan 1 bit
cout << "Nilai x kini = " << x << '\n';
getche();
}
Hasil Program:
Program 3.9
//* *
//* KOMPLEMEN.C *
//* *
//* Melihat efek operator komplemen*
//* *
#include <iostream.h>
#include <conio.h>
void main ()
{
unsigned short int nilai = 81;
unsigned short int a;
clrscr () ;
Hasil Program:
Program 3.10
//* *
//* bit6.cpp *
//* *
//* Contoh program untuk mengetahui nilai bit 6 *
//* dari data bertipe integer. *
//* *
#include <iostream.h>
#include <conio.h>
void main ()
{
unsigned int nilai, bit;
nilai = 81;
bit = (nilai >> 6) & 1;
cout << "Nilai bit = " << bit;
nilai = 80;
bit = (nilai << 6) & 1;
Hasil Program:
Program 3.11
//* *
//* ubahbit7.cpp *
//* *
//* Contoh program untuk mengubah bit 7 *
//* dari data bertipe integer. *
//* agar bernilai 1 *
//* *
#include <iostream.h>
#include <conio.h>
void main ()
{
unsigned char nilai = 81;
clrscr () ;
Hasil Program:
Program 3.12
//* *
//* majemuk.cpp *
//* *
//* Contoh beberapa operasi dengan operator majemuk. *
//* *
#include <iostream.h>
#include <conio.h>
void main ()
{
int x = 2 ; // Mula-mula x bernilai 2
clrscr ();
cout << "x =" << x << '\n';
x += 3;
cout << "Setelah x += 3, x -> " << x << '\n';
x *= 2;
cout << "Setelah x *= 2, x -> " << x << '\n';
getche ();
}
Hasil Program:
Program 3.13
//* *
//* kondisi.cpp*
//* *
//* Contoh untuk menunjukkan nilai *
//* hasil ungkapan kondisi *
//* *
#include <iostream.h>
#include <conio.h>
void main ()
{
int nilai;
clrscr ();
Hasil Program:
Program 3.14
//* *
//* logika.cpp *
//* *
//* Contoh pemakaian operator logika *
//* && dan ||. *
//* *
#include <conio.h>
void main ()
{
int x = 200;
clrscr ();
cout << "(x >= 1) && (x <= 50) -> " << ((x >= 1) && (x <= 50) << '\n';
cout << "(x >= 1) || (x <= 50) -> " << ((x >= 1) || (x <= 50) << '\n';
getche ();
}
Hasil Program:
Program 3.15
//* *
//* opkond.cpp *
//* *
//* Contoh penggunaan operator kondisi *
//* untuk memperoleh bilangan terkecil *
//* diantara dua buah bilangan. *
//* *
#include <iostream.h>
#include <conio.h>
void main ()
{
int bil1, bil2, minim;
clrscr ();
bil1 = 53;
bil2 =6;
Hasil Program:
Program 3.16
//* *
//* opkoma.cpp *
//* *
//* Contoh program yang memperlihatkan hasil *
//* ungkapan dengan operator koma. *
//* *
#include <iostream.h>
#include <conio.h>
void main ()
{
int hasil;
clrscr ();
Hasil Program :
Program 3.17
//* *
//* otokonv.cpp *
//* *
//* Contoh ungkapan yang melibatkan beberapa tipe data. *
//* *
#include <iostream.h>
#include <conio.h>
void main ()
{
int jumlah;
float harga_per_unit;
double harga_total;
clrscr ();
jumlah = 5;
harga_per_unit = 5203.02;
harga_total = harga_per_unit * jumlah;
Hasil Program:
Program 3.18
//* *
//* ascii.cpp *
//* *
//* Menampilkan kode ASCII dari suatu karakter *
//* melalui konversi tipe. *
//* *
#include <iostream.h>
#include <conio.h>
void main ()
{
char kar = 'A';
clrscr ();
cout << "Nilai ASCII dari " << kar << "yaitu " << int (kar) << '\n';
getche ();
}
Hasil Program :
Program 3.19
//* *
//* konversi.cpp *
//* *
//* Contoh konversi pada penugasan ke variabel. *
//* *
#include <iostream.h>
#include <conio.h>
void main ()
{
char c;
int i;
float x;
clrscr ();
x = 176.5; // penugasan 1
i = x; // penugasan 2
c = i; // penugasan 3
cout << "Isi x = " << x << '\n';
cout << "Isi i = " << i << '\n';
cout << "Isi c = " << c << '\n';
getche ();
}
Hasil Program :
Program 3.20
//* *
//* akarkuad.cpp *
//* *
//* Contoh pemakaian pustaka fungsi sqrt(). *
//* *
#include <iostream.h>
#include <math.h> // Perlu disertakan untuk fungsi sqrt()
#include <conio.h>
void main ()
{
clrscr ();
cout << "Akar dari 27 = " << sqrt (27) << '\n';
getche ();
}
Hasil Program :