ปัญหา
โดยไม่ต้องใช้สวิตช์เคส คุณจะพิมพ์ตัวเลขที่ระบุโดยใช้ภาษาซีได้อย่างไร
วิธีแก้ปัญหา
ในโปรแกรมนี้ เรากำลังตรวจสอบเงื่อนไขสามข้อเพื่อพิมพ์ตัวเลขสองหลักเป็นคำ −
-
if(no<0 || no>99)
ตัวเลขที่ป้อนไม่ใช่ตัวเลขสองหลัก
-
อื่น if(no==0)
พิมพ์ตัวเลขแรกเป็นศูนย์
-
อื่น if(no>=10 &&no<=19)
พิมพ์ตัวเลขหลักเดียวเป็นคำ
-
อื่น if(no>=20 &&no<=90)
if(ไม่มี%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\n");
else if(no==0)
printf("the enter no is:%s\n",firstno[no]);
else if(no>=10 && no<=19)
printf("the enter no is:%s\n",firstno[no-10+1]);
else if(no>=20 && no<=90)
if(no%10 == 0)
printf("the enter no is:%s\n",secondno[no/10 - 2]);
else
printf("the enter no is:%s %s\n",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