Anda di halaman 1dari 21

BAB 3

OPERATOR DAN UNGKAPAN


Program 3.1

//* *
//* 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;

cout << "d = " << d << '\n';


getche();
}

Hasil Program:
Program 3.2
//* *
//* modulus.cpp *
//* *
//* Contoh untuk melihat sisa pembagian dengan *
//* menggunakan operator %. *
//* *

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

void main ()
{
clrscr () ;

cout << 5 % 7 << '\n'; // Sisa 5


cout << 6 % 7 << '\n'; // Sisa 6
cout << 7 % 7 << '\n'; // Sisa 0
cout << 8 % 7 << '\n'; // Sisa 1
cout << 9 % 7 << '\n'; // Sisa 2

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 ()
{

unsigned int x = 93;


clrscr ();
cout << "Nilai x semula = "<< x <<'\n';
x = x << 1 ; // geser ke kiri 1 bit
cout << "Nilai x kini = "<< x <<'\n';
getche();
}
Hasil Program:
Program 3.7

//* *
//* 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 () ;

a = ~nilai; // komplemen dari nilai


cout << "a = " << a << '\n';
getche ();
}

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;

cout << "Nilai bit = " << bit;


getche ();
}

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 () ;

cout << "Nilai semula = " << nilai;


nilai = nilai || 81 ;
cout << "Nilai kini = " << nilai;
getche ();
}

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 ();

nilai = 3 > 2; // Hasil ungkapan : benar


cout << "nilai = " << nilai << endl;

nilai = 2 > 3; // Hasil ungkapan : salah


cout << "nilai = " << nilai << endl;
getche ();
}

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;

minim = bil1 < bil2 ? bil1 : bil2 ;

cout << "Bilangan terkecil = " << minim << '\n';


getche ();
}

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 = 2 > 3, 3 > 2; // Hasil berupa benar / angka 1


cout << "Hasil ungkapan: hasil = 2 > 3, 3 > 2 --> "<<hasil << '\n';

hasil = 3 > 2, 2 > 3; // Hasil berupa benar / angka 0


cout << "Hasil ungkapan: hasil = 3 > 2, 2 > 3 --> " <<hasil << '\n';
getche ();
}

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;

cout << "harga total = " << harga_total << '\n';


getche ();
}

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 :

Anda mungkin juga menyukai