Display the digits of an integer using words

image_pdfimage_print

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Display the digits of an integer using words. 
 
using System; 
 
public class ConvertDigitsToWords {   
  public static void Main() { 
    int num; 
    int nextdigit; 
    int numdigits; 
    int[] n = new int[20]; 
     
    string[] digits = { "zero", "one", "two", 
                        "three", "four", "five", 
                        "six", "seven", "eight", 
                        "nine" }; 
 
    num = 1908; 
 
    Console.WriteLine("Number: " + num); 
 
    Console.Write("Number in words: "); 
     
    nextdigit = 0; 
    numdigits = 0; 
   
    /* Get individual digits and store in n. 
       These digits are stored in reverse order. */ 
    do { 
      nextdigit = num % 10; 
      n[numdigits] = nextdigit; 
      numdigits++; 
      num = num / 10; 
    } while(num > 0); 
    numdigits--; 
 
    // display words 
    for( ; numdigits >= 0; numdigits--) 
      Console.Write(digits[n[numdigits]] + " "); 
 
    Console.WriteLine(); 
  }   
}

           
          


This entry was posted in Data Types. Bookmark the permalink.