We are in a position to write some programs on strings now.
Example
21: Write a C program to write a given figure in words. Use the switch statement.
Solution 21:
The program is as follows:
#include
main( )
{
int digit[10], num, temp, i, j, k;
printf(“\n\t Enter your number:”);
printf(“%d\n”, num);
temp = num;
i = 0;
while (num != 0)
{
digit[i++] = num – num/10 * 10;
num = num/10;
}
j = - - i;
printf (“\nThe number in words is:”);
for (k=j; k>=0; k - - )
{
switch (digit[k])
{
case 1:
printf(“one”);
break;
case 2:
printf(“two”);
break;
case 3:
printf(“three”);
break;
case 4:
printf(“four”);
break;
case 5:
printf(“five”);
break;
case 6:
printf(“six”);
break;
case 7:
printf(“seven”);
break;
case 8:
printf(“eight”);
break;
case 9:
printf(“nine”);
break;
case 10:
printf(“ten”);
break;
default:
break;
}
}
printf(“\n”);
}
Do'stlaringiz bilan baham: |