Thread: Program to convert 2 digit number to corresponding english word

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Program to convert 2 digit number to corresponding english word

    Hi everyone,
    Just started learning C.I have to create a program using only switch case to convert 2 digit number to their respective english word.This is code i came up with..
    Code:
    # include <stdio.h>
    main()
    {
          int digit;
          printf("Enter a two-digit number:");
          scanf("%d",& digit);
          
          if(digit<10 || digit>99)
          {printf("Incorrect value!Please enter the two-digit number again.");}
          
          printf("You have entered the number ");
                     
          switch (digit/10)
          {
           case 2:printf("Twenty-");
           break;
           case 3:printf("Thirty-");
           break;
           case 4:printf("Forty-");
           break;
           case 5:printf("Fifty-");
           break;
           case 6:printf("Sixty-");
           break;
           case 7:printf("Seventy-");
           break;
           case 8:printf("Eighty-");
           break;
           case 9:printf("Ninety-");
           break;
           }
     switch (digit%10)
           {
           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;
           }
    return 0;
    }
    But in this code i cannot print nos from 10 to 19.Can anybody suggest me ways to do it?
    Thanks a lot!!

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    add a case 1 to the first switch and have a nested switch for all the different ten eleven twelve etc, and have the 2nd switch inside an if (digit<10 || digit>19)

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use an array of 100 strings and call it directly by index!
    Code:
    printf( "You entered \'%s\'!\n", numbers[ x ] );



    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Thumbs up

    Thanks EPY!!It worked!!I didnt know switch could be nested!learnt something new!!
    @quzah, we havent gone uptil array...but will definitely try it out once i learn more about arrays!!Thanks for the advice though!!
    Code:
    # include <stdio.h>
    main()
    {
          int digit;
          printf("Enter a two-digit number:");
          scanf("%d",& digit);
          
          if(digit<10 || digit>99)
          {printf("Incorrect value!Please enter the two-digit number again.\n");}
          
          printf("You have entered the number ");
                     
          switch (digit/10)
          {
           case 1:
                switch (digit)
                {
           case 10:printf("Ten");            
           case 11:printf("Eleven");
           break;
           case 12:printf("Twelve");
           break;
           case 13:printf("Thirteen");
           break;
           case 14:printf("Fourteen");
           break;
           case 15:printf("Fifteen");
           break;
           case 16:printf("Sixteen");
           break;
           case 17:printf("Seventeen");
           break;
           case 18:printf("Eighteen");
           break;
           case 19:printf("Nineteen");
           break;
           }
           break;
           case 2:printf("Twenty-");
           break;
           case 3:printf("Thirty-");
           break;
           case 4:printf("Forty-");
           break;
           case 5:printf("Fifty-");
           break;
           case 6:printf("Sixty-");
           break;
           case 7:printf("Seventy-");
           break;
           case 8:printf("Eighty-");
           break;
           case 9:printf("Ninety-");
           break;
           }
           
           if (digit<10 || digit>19)
           {{switch (digit%10)
           {
           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;
           }
           }
           return 0;
     }
    }
    Yay!!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by renegadewarrior View Post
    @quzah, we havent gone uptil array...but will definitely try it out once i learn more about arrays!!Thanks for the advice though!!
    It wasn't so much advise as it was an alternate way of doing it. It probably wasn't what your teacher had in mind.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. Program that requests input of an integer number
    By theejuice in forum C Programming
    Replies: 6
    Last Post: 04-30-2008, 02:18 AM
  4. Replies: 4
    Last Post: 07-14-2003, 08:11 AM
  5. Program to show the number of occurrences of words...
    By Nutshell in forum C Programming
    Replies: 5
    Last Post: 01-26-2002, 06:44 PM