在不使用 switch case 的情況下,如何使用 C 程式語言以文字形式列印給定的數字?
在這個程式中,我們檢查三個條件以用單字列印兩位數-
if(no 99)
if(no99) p>
輸入的數字不是兩位數
else if( no==0)
將第一個數字印為零
else if(no>=10 && no
用文字印個位數
else if(no>=20 && no
if(no == 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</p><p>"); else if(no==0) printf("the enter no is:%s</p><p>",firstno[no]); else if(no>=10 && no<=19) printf("the enter no is:%s</p><p>",firstno[no-10+1]); else if(no>=20 && no<=90) if(no%10 == 0) printf("the enter no is:%s</p><p>",secondno[no/10 - 2]); else printf("the enter no is:%s %s</p><p>",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
以上是編寫一個C程序,使用elseif語句將數字列印為單字的詳細內容。更多資訊請關注PHP中文網其他相關文章!