#Diktat Alpro Bab 4 - Latihan No.8
Latihan Kondisi
8. Diberikan ketentuan pecahan uang di Indonesia. User memasukkan sebuah
bilangan, program harus dapat memecahnya menjadi pecahan sesuai dengan
mata uang di Indonesia.
Program:
#include <iostream>
using namespace std;
void first (int n) {
switch (n) {
case 1: cout << "satu "; break;
case 2: cout << "dua "; break;
case 3: cout << "tiga "; break;
case 4: cout << "empat "; break;
case 5: cout << "lima "; break;
case 6: cout << "enam "; break;
case 7: cout << "tujuh "; break;
case 8: cout << "delapan "; break;
case 9: cout << "sembilan "; break;
case 10: cout << "sepuluh "; break;
case 11: cout << "sebelas "; break;
default: break;
}
}
void second (int n) {
int bul, sisa;
bul = n / 10;
sisa = n % 10;
if (bul == 0)
first (sisa);
else if (bul == 1) {
if (sisa <= 1)
first (n);
else {
first (sisa);
cout << "belas ";
}
}
else {
first (bul);
cout << "puluh ";
first (sisa);
}
}
void third (int n) {
int bul, sisa;
bul = n / 100;
sisa = n % 100;
if (bul == 0)
second (sisa);
else if (bul == 1) {
if (sisa == 0)
cout << "seratus ";
else {
cout << "seratus ";
second (sisa);
}
}
else {
first (bul);
cout << "ratus ";
second (sisa);
}
}
void fourth (int n) {
int bul, sisa;
bul = n / 1000;
sisa = n % 1000;
if (bul == 0)
third (sisa);
else if (bul == 1) {
if (sisa == 0)
cout << "seribu ";
else {
cout << "seribu ";
third (sisa);
}
}
else {
third (bul);
cout << "ribu ";
third (sisa);
}
}
void fifth (int n) {
int bul, sisa;
bul = n / 1000000;
sisa = n % 1000000;
if (bul == 0)
fourth (sisa);
else if (bul == 1) {
if (sisa == 0)
cout << "satu juta ";
else {
cout << "satu juta ";
fourth (sisa);
}
}
else {
third (bul);
cout << "juta ";
fourth (sisa);
}
}
int main () {
int n = 1;
while (n == 1) {
int num1;
do {
cout << "";
cin >> num1;
} while (num1 < 1);
fifth (num1);
cout << "\n";
}
}
Algoritma:
1.
Algoritma
membaca nilai bilangan dengan Pangkat
{Diberikan masukan x dan y, dihitung nilai dari x pangkat y}
Deklarasi
Deklarasi
x, y, i : integer { input }
pangkat : integer { output }
Deskripsi
Deskripsi
read (x,y)
pangkat ß 1
for i ß 1
to y do
pangkat ß pangkat * x
enfor
write
(pangkat)
0 komentar:
Posting Komentar