พิมพ์ค่าตัวเลขที่กำหนดเป็นคำ ทำได้ง่ายๆ ด้วยสวิตช์โดยใช้เคสตั้งแต่ 0-9 แต่ความท้าทายคือไม่ต้องใช้เคส
ป้อนข้อมูล − N=900
ผลผลิต − เก้าศูนย์ศูนย์
เป็นไปได้โดยการสร้างอาร์เรย์ของพอยน์เตอร์ที่มี 0-9 ในคำ
อัลกอริทึม
START
Step 1 -> declare int variables num, i and array of pointer char *alpha with values {"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"}
Step 2 -> declare char array str[20]
Step 3 -> call function itoa with parameters num,str,10
Step 4 -> Loop For i=0 and str[i]!=’\o’ and i++
Print alpha[str[i] - '0']
Step 5 -> End Loop
STOP ตัวอย่าง
#include<stdio.h>
#include<stdlib.h>
int main() {
int num, i;
num=900; //lets take numeric value
char *alpha[11] = {"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"};
char str[20];
itoa(num, str, 10); //this function will convert integer to alphabet
for(i=0; str[i] != '\0'; i++)
printf("%s ", alpha[str[i] - '0']);
return 0;
} ผลลัพธ์
หากเรารันโปรแกรมด้านบน มันจะสร้างผลลัพธ์ดังต่อไปนี้
Enter an integer 900 NINE ZERO ZERO