Yêu cầu:
Viết chương trình hướng dẫn đọc số tiếng anh
– Nhập vào 1 số có 3 chữ số
– Hiển thị cách đọc ra màn hình
Ví dụ: 2014 -> two zero one four
Giải thuật:
– Dùng switch case hoặc if/else
Code
/************************************************************ * Author: VNCODING * History * 2016/12/19 first create VNCODING *************************************************************/ #include <stdio.h> #include <conio.h> const char *number_table[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"}; int main() { int num; do { printf("\nnum = "); scanf("%d", &num); }while(num < 100 || num > 999); printf("%s %s %s", number_table[num/100], number_table[num/10%10], number_table[num%10]); getch(); return 0; }
Kết quả:
printf(“%s %s %s”, number_table[num/100], number_table[num/10%10], number_table[num%10]);
ai có thể giải thích đoạn code này được không ạ. Cảm ơn
@khoa,
Ví dụ: nhập vào 345
num/100 = 345/100 = 3 –> number_table[3] = “three”
num/10%10 = 345/10%10 = 4 –> number_table[4] = “four”
…