Print a number in words

© Parineeth M R

Question 26. Given a number from 0 to 999,999,999, print the number in words. So 200,145,700 should be printed as two hundred million one hundred forty five thousand seven hundred


We will break the input number into millions, thousands and the remaining 3 least significant digits. So in this case 200,145,700 gets broken down to 200, 145 and 700. Then we make use of a helper function that prints 3 consecutive digits.

C/C++


/* 
Helper function to print number from 1 to 999
number: number from 1 to 999
*/
void print_3_digits(int number)
{
	int hundreds_digit, tens_digit, unit_digit, remainder;

	/*basic_lookup[0] is empty. We want basic_lookup[1] to map to "One" 
	and so on. */
	char *basic_lookup[] = {"", "One", "Two", "Three", "Four", "Five", "Six",
			"Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", 
			"Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen",
			"Eighteen", "Nineteen"};

	/*tens_lookup[0] and tens_lookup[1] are empty.
	We want tens_lookup[2] to map to "Twenty" and so on. */
	char *tens_lookup[] = {"", "","Twenty", "Thirty", "Fourty", "Fifty", 
			"Sixty", "Seventy", "Eighty", "Ninety"};

	/*Suppose number is 987, then hundreds_digit is 9*/
	hundreds_digit = number / 100;
	if (hundreds_digit > 0) {
		printf("%s Hundred ", basic_lookup[hundreds_digit]);
	}
	
	/*Suppose number is 987, then remainder will be 87*/
	remainder = number % 100;
	if (remainder > 0) {
		if (remainder <= 19) {
			printf("%s ", basic_lookup[remainder]);			
		} else {
			tens_digit = remainder / 10;
			unit_digit = remainder % 10;
			printf("%s ", tens_lookup[tens_digit]);
			printf("%s ", basic_lookup[unit_digit]);
		}
	}
	
}
 
/*
Main function to print the number in words
number: any number from 0 to 999999999
*/
void print_num_in_words(int number)
{
	int millions, thousands, remainder;

	/*If number is 0, handle it here and return*/
	if (number == 0) {
		printf("Zero \n");
		return;
	}

	/*Suppose number is 123456789, then millions = 123, remainder = 456789*/
	millions = number / 1000000;
	remainder = number - (millions * 1000000);
	
	/*Suppose remainder = 456789, then thousands = 456, remainder = 789*/
	thousands = remainder / 1000;
	remainder = remainder - (thousands * 1000);

	if (millions > 0) {
		print_3_digits(millions);
		printf("Million ");
	}

	if (thousands > 0) {
		print_3_digits(thousands);
		printf("Thousand ");
	}

	if (remainder > 0) {
		print_3_digits(remainder);
	} 
	
	printf("\n");
}



Java


/* 
Helper function to print number from 1 to 999
number: number from 1 to 999
*/
public static void print3Digits(int number) {
	/*basicLookup[0] is empty. We want basicLookup[1] to map to "One"
	and so on. */
	String[] basicLookup = {"", "One", "Two", "Three", "Four", "Five", 
			"Six", "Seven", "Eight", "Nine", "Ten", "Eleven", 
			"Twelve", "Thirteen", "Fourteen", "Fifteen", 
			"Sixteen", "Seventeen",	"Eighteen", "Nineteen"};

	/*tensLookup[0] and tensLookup[1] are empty.
	We want tensLookup[2] to map to "Twenty" and so on. */
	String[] tensLookup = {"", "","Twenty", "Thirty", "Fourty", "Fifty",
			"Sixty", "Seventy", "Eighty", "Ninety"};

	/*Suppose number is 987, then hundredsDigit is 9*/
	int hundredsDigit = number / 100;
	if (hundredsDigit > 0) {
		System.out.print(basicLookup[hundredsDigit] + " Hundred ");
	}

	/*Suppose number is 987, then remainder will be 87*/
	int remainder = number % 100;
	if (remainder > 0) {
		if (remainder <= 19) {
			System.out.print(basicLookup[remainder] + " ");			
		} else {
			int tensDigit = remainder / 10;
			int unitDigit = remainder % 10;
			System.out.print(tensLookup[tensDigit] + " ");
			System.out.print(basicLookup[unitDigit] + " ");
		}
	}
}
 
/*
Main function to print the number in words
number: any number from 0 to 999999999
*/
public static void printNumInWords(int number) {
	/*If number is 0, handle it here and return*/
	if (number == 0) {
		System.out.println("Zero ");
		return;
	}

	/*Suppose number is 123456789, then millions = 123, remainder = 456789*/
	int millions = number / 1000000;
	int remainder = number - (millions * 1000000);

	/*Suppose remainder = 456789, then thousands = 456, remainder = 789*/
	int thousands = remainder / 1000;
	remainder = remainder - (thousands * 1000);

	if (millions > 0) {
		print3Digits(millions);
		System.out.print("Million ");
	}

	if (thousands > 0) {
		print3Digits(thousands);
		System.out.print("Thousand ");
	}

	if (remainder > 0) {
		print3Digits(remainder);
	} 

	System.out.println("");
}



Python


#Helper function to print number from 1 to 999
#number: number from 1 to 999
def print_3_digits(number) :
	#basic_lookup[0] is empty. We want basic_lookup[1] to map to 'One'
	#and so on. 
	basic_lookup = ['', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 
			'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve',
			'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 
			'Seventeen', 'Eighteen', 'Nineteen']

	#tens_lookup[0] and tens_lookup[1] are empty.
	#We want tens_lookup[2] to map to 'Twenty' and so on. 
	tens_lookup = ['', '','Twenty', 'Thirty', 'Fourty', 'Fifty', 'Sixty',
			'Seventy', 'Eighty', 'Ninety']

	#Suppose number is 987, then hundreds_digit is 9
	hundreds_digit = number // 100
	if (hundreds_digit > 0) :
		print(basic_lookup[hundreds_digit] + ' Hundred ', end='')
	

	#Suppose number is 987, then remainder will be 87
	remainder = number % 100
	if (remainder > 0) :
		if (remainder <= 19) :
			print(basic_lookup[remainder] + ' ', end='')
		else :
			tens_digit = remainder // 10
			unit_digit = remainder % 10
			print(tens_lookup[tens_digit] + ' ', end='')
			print(basic_lookup[unit_digit] + ' ', end='')
 
#Main function to print the number in words
#number: any number from 0 to 999999999
def print_num_in_words(number) :
	#If number is 0, handle it here and return
	if (number == 0) :
		print('Zero ')
		return
	
	#Suppose number is 123456789, then millions = 123, remainder = 456789
	millions = number // 1000000
	remainder = number - (millions * 1000000)

	#Suppose remainder = 456789, then thousands = 456, remainder = 789
	thousands = remainder // 1000
	remainder = remainder - (thousands * 1000)

	if (millions > 0) :
		print_3_digits(millions)
		print('Million ', end='')
	
	if (thousands > 0) :
		print_3_digits(thousands)
		print('Thousand ', end='')
	
	if (remainder > 0) :
		print_3_digits(remainder)
	 
	print('')