编写 C 程序以使用 elseif 语句以文字形式打印数字


问题

在不使用 switch case 的情况下,如何使用 C 编程语言以文字形式打印给定数字?

解决方案

在此程序中,我们正在检查三个条件以以文字形式打印两位数 −

  • if(no<0 || no>99)

    输入的数字不是两位数

  • else if(no==0)

    将第一个数字打印为零

  • else if(no>=10 && no<=19)

    以文字形式打印个位数

  • else if(no>=20 && no<=90)

         if(no%10 == 0)

    以文字形式打印两位数

程序

 实时演示

#include<stdio.h>
#include<string.h>
int main(){
   int no;
   char *firstno[]={"zero","ten","eleven","twelve","thirteen", "fourteen","fifteen","sixteen","seventeen", "eighteen","nineteen"};
   char *secondno[]={"twenty","thirty","forty","fifty","sixty", "seventy","eighty","ninty"};
   char *thirdno[]={"one","two","three","four","five","six","seven","eight","nine"};
   printf("enter a number:");
   scanf("%d",&no);
   if(no<0 || no>99)
      printf("enter number is not a two digit number
");    else if(no==0)       printf("the enter no is:%s
",firstno[no]);    else if(no>=10 && no<=19)       printf("the enter no is:%s
",firstno[no-10+1]);    else if(no>=20 && no<=90)       if(no%10 == 0)          printf("the enter no is:%s
",secondno[no/10 - 2]);    else       printf("the enter no is:%s %s
",secondno[no/10-2],thirdno[no%10-1]); return 0; }

输出

enter a number:79
the enter no is: seventy nine
enter a number:234
enter number is not a two digit number

更新于: 05-Mar-2021

4K+ 浏览次数

开启 职业生涯

完成课程以获得认证

开始
广告