public String kata(int number){}
number = Math.abs(number);
String [] angka = {“”,”satu”,”dua”,”tiga”,”empat”,”lima”,”enam”,”tujuh”,”delapan”,”sembilan”,”sepuluh”,”sebelas”};
String temp = “”;
if(number < 12){
temp = ” “+angka[number];
}else if(number < 20){
temp = kata(number – 10)+” belas”;
}else if(number < 100){
temp = kata(number/10)+” puluh”+kata(number%10);
} else if (number < 200) {
temp = ” seratus”+kata(number – 100);
} else if (number < 1000) {
temp = kata(number/100)+ ” ratus”+ kata(number % 100);
} else if (number < 2000) {
temp = ” seribu”+ kata(number – 1000);
} else if (number < 1000000) {
temp = kata(number/1000)+” ribu”+ kata(number % 1000);
} else if (number < 1000000000) {
temp = kata(number/1000000)+” juta”+ kata(number % 1000000);
}
return temp;
C++
void terbilang(long n, char *str) {
if(n < 10)
strcat(str, kata[n]);
else if(n >= 1000000000) {
terbilang(n / 1000000000, str);
strcat(str, " milyar ");
terbilang(n % 1000000000, str);
}
else if(n >= 1000000) {
terbilang(n / 1000000, str);
strcat(str, " juta ");
terbilang(n % 1000000, str);
}
else if(n >= 1000) {
if(n / 1000 == 1) {
strcat(str, "seribu ");
terbilang(n % 1000, str);
}
else {
terbilang(n / 1000, str);
strcat(str, " ribu ");
terbilang(n % 1000, str);
}
}
else if(n >= 100) {
if(n / 100 == 1) {
strcat(str, "seratus ");
terbilang(n % 100, str);
}
else {
terbilang(n / 100, str);
strcat(str, " ratus ");
terbilang(n % 100, str);
}
}
else if(n >= 10) {
if(n == 10)
strcat(str, "sepuluh");
else if(n == 11)
strcat(str, "sebelas");
else if(n >= 20) {
terbilang(n / 10, str);
strcat(str, " puluh ");
terbilang(n % 10, str);
}
else {
terbilang(n % 10, str);
strcat(str, " belas");
}
}
Tidak ada komentar:
Posting Komentar