Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to convert Digits to Words
Converting digits to words in C# involves breaking down a number into its individual digits and mapping each digit to its corresponding word representation. This technique is commonly used in financial applications, report generation, and number-to-text conversion systems.
Algorithm Overview
The conversion process follows these steps −
Create an array of word strings for digits 0-9
Extract each digit from the number using modulo operation
Store digits in reverse order (rightmost digit first)
Map each digit to its corresponding word and display in correct order
Basic Implementation
Here's a simple approach to convert digits to words −
using System;
class DigitToWords {
static void Main(string[] args) {
int val, next, num_digits;
int[] a = new int[10];
// words for every digits from 0 to 9
string[] digits_words = {
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"
};
// number to be converted into words
val = 4677;
Console.WriteLine("Number: " + val);
Console.Write("Number (words): ");
next = 0;
num_digits = 0;
// extract digits and store in reverse order
do {
next = val % 10;
a[num_digits] = next;
num_digits++;
val = val / 10;
} while (val > 0);
// display words in correct order
num_digits--;
for (; num_digits >= 0; num_digits--)
Console.Write(digits_words[a[num_digits]] + " ");
}
}
The output of the above code is −
Number: 4677 Number (words): four six seven seven
Using String-Based Approach
An alternative method using string manipulation for better readability −
using System;
class StringDigitConverter {
static void Main(string[] args) {
string[] digitWords = {
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"
};
int number = 2534;
Console.WriteLine("Number: " + number);
string numberStr = number.ToString();
Console.Write("Number (words): ");
foreach (char digit in numberStr) {
int digitValue = digit - '0';
Console.Write(digitWords[digitValue] + " ");
}
Console.WriteLine();
}
}
The output of the above code is −
Number: 2534 Number (words): two five three four
Method-Based Implementation
A more organized approach using separate methods −
using System;
class DigitWordConverter {
static string[] digitWords = {
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"
};
static string ConvertDigitsToWords(int number) {
if (number == 0) return "zero";
string result = "";
string numStr = number.ToString();
foreach (char digit in numStr) {
int digitValue = digit - '0';
result += digitWords[digitValue] + " ";
}
return result.Trim();
}
static void Main(string[] args) {
int[] testNumbers = { 123, 4056, 789, 0 };
foreach (int num in testNumbers) {
Console.WriteLine($"Number: {num}");
Console.WriteLine($"Words: {ConvertDigitsToWords(num)}");
Console.WriteLine();
}
}
}
The output of the above code is −
Number: 123 Words: one two three Number: 4056 Words: four zero five six Number: 789 Words: seven eight nine Number: 0 Words: zero
Key Points
The modulo operator (%) extracts the rightmost digit
Integer division (/) removes the rightmost digit
Digits are processed in reverse order, so they need to be stored and then displayed in correct sequence
String conversion approach is simpler but may be less efficient for very large numbers
Conclusion
Converting digits to words in C# can be accomplished through mathematical digit extraction or string manipulation. The mathematical approach provides better understanding of number processing, while the string-based method offers cleaner, more readable code for most practical applications.
