C# Program to Convert Digits to Words
Posted by Superadmin on August 11 2022 08:23:03

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.

 

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