編寫 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+ 瀏覽次數

開啟 職業生涯

完成課程以獲得認證

開始
廣告