不使用 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