Thursday, 7 September 2017

Write a recursive function that displays a positive integer in words.



Write a recursive function that displays a positive integer in words. 
Ex: if the integer is 465 then it is displayed as: FOUR SIX FIVE

Program:
#include <stdio.h>
void rec(int n)
{
               if((n/10)>0)
                              rec(n/10);
               int r=n%10;
               switch(r)
               {
                              case 0:
                                                            printf("zero");
                                                            break;
                              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;
               }
}
main()
{
               int n;
               printf("enter a number");
               scanf("%d",&n);
               rec(n);
}

Output

 

No comments:

Post a Comment