不使用 if 或 switch 輸出單詞表示的單個數字的 C 程式。
將給定的數字值列印為單詞。使用 0-9 的 switch 很容易做到,但如果不使用它們則有難度。
輸入 - 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP