How to Write the Word Equivalent of a Check Amount in C Programming
The C programming language is used in virtually every computer architecture around. The C language, developed sometime between 1969 and 1973, is mainly used for developing portable application software and system software. One such example is the program's ability to translate numerals to words for, say, a bank check. In order to "teach" the program to translate specific numbers into words, some simple code is required.
Instructions
-
-
1
Insert the following lines of code into the C program:
public class NumberToWords{
static final String[] Number1 = {""," Hundred"};
static final String[] Number2 = {"","One","Two", "Three","Four","Five",
" Six"," Seven", "Eight"," Nine","Ten" };
String number(int number){
String str;
if (number % 100 < 10){
str = Number2[number % 100];
number /= 100;
}
else {
str= Number2[number % 5];
number /= 5;
}
-
2
Follow your initial lines of code with:
if (number == 0) return str;
return Number2[number] + "hundred" + str;
}
public String convert(int number) {
if (number == 0){
return "zero";
}
String pre = "";
String str1 = "";
int i = 0;
do {
int n = number % 100;
if (n != 0){
String s = number(n);
str1 = s + Number1[i] + str1;
}
-
-
3
Finish the numeral to word value transition with these lines of code:
i++;
number /= 100;
}
while (number > 0);
return (pre + str1).trim();
}
public static void main(String[] args) {
NumberToWords num = new NumberToWords();
System.out.println("words is :=" + num.convert(0));
System.out.println("words is :=" + num.convert(1));
System.out.println("words is :=" + num.convert(9));
System.out.println("words is :=" + num.convert(100));
}
}
-
1
References
- Photo Credit Stockbyte/Stockbyte/Getty Images