C# Program to Convert Digits to Words

This is a C# Program to convert digits to words.

Problem Description

This C# Program Converts Digits to Words.

Problem Solution

Here the user enters the number which is again converted and diplayed in terms of words with the help of a mod function.

Program/Source Code

Here is source code of the C# Program to Convert Digits to Words. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 *  C# Program to Convert Digits to 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" }; 
 
 
    Console.WriteLine("Enter the number");
    num = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("Number: " + num); 
    Console.Write("Number in words: ");    
    nextdigit = 0; 
    numdigits = 0; 
    do 
    { 
      nextdigit = num % 10; 
      n[numdigits] = nextdigit; 
      numdigits++; 
      num = num / 10; 
    } while(num > 0); 
    numdigits--; 
    for( ; numdigits >= 0; numdigits--) 
        Console.Write(digits[n[numdigits]] + " "); 
    Console.WriteLine();
    Console.ReadLine();
  }   
}
Program Explanation

This C# program, we are reading the number using ‘num’ variable. Using do while loop Compute the modulus of the value of ‘num’ variable by 10. Assign the resulted value to ‘nextdigit’ variable and increment the value of ‘numdigit’ variable.

advertisement
advertisement

Compute the value of ‘num’ variable by 10. While loop is used to check the value of ‘num’ variable is greater than 0. If the condition is true, then execute the iteration of the loop. For loop is used to convert the digits to words and print the output of the program.

Runtime Test Cases
 
Enter the number
1548
Number: 1548
Number in words: one five four eight

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.