Display the digits of an integer using words : String : Data Types C# Source Code


Custom Search

C# Source Code » Data Types » String »

 

Display the digits of an integer using words









    

/*
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(); 
  }   
}

           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Data Types
» String