elseifステートメントを使用して数字を単語で出力するCプログラムを作成する
問題
スイッチケースを使用せずに、Cプログラミング言語を使用して特定の数値を単語で印刷するにはどうすればよいですか?
解決策
このプログラムでは、2桁の数字を単語で印刷するための3つの条件をチェックしています-
-
if(no <0 || no> 99)
入力した数字は2桁ではありません
-
else if(no ==0)
最初の数値をゼロとして出力
-
else if(no> =10 &&no <=19)
1桁の数字を単語で印刷する
-
else if(no> =20 &&no <=90)
if(no%10 ==0)
2桁の数字を単語で印刷する
プログラム
#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
-
与えられた数を単語に変換するCプログラム
数値で構成される文字列が与えられた場合、タスクはそれらの与えられた数値を単語で隠すことです。 入力「361」があるように。その場合、出力は「三百六十一」という言葉である必要があります。次の問題を解決するには、1、数万、数千などの数と場所を覚えておく必要があります。 コードは最大4桁の数字、つまり0〜9999のみをサポートします。したがって、入力は0〜9999である必要があります。 場所が-のようになるように1,111を考えてみましょう 例 Input: “1234” Output: one thousand two hundred thirty four
-
Cプログラムで行列の対角パターンで数値を印刷します。
タスクは、対角パターンのnxnの行列を印刷することです。 nが3の場合、対角パターンで行列を印刷するのは-です。 したがって、出力は次のようになります- 例 Input: 3 Output: 1 2 4 3 5 7 6 8 9 Input: 4 Output: 1 2 4 7 3 5 8 11 6 9 12 14 10 13 15 16 この問題は、数値nを与え、n x nの行列を生成