Program to convert a given number to words
Write code to convert a given number into words. For example, if “1234” is given as input, the output should be “one thousand two hundred thirty-four”.
Following is the implementation for the same. The code supports numbers up to 4 digits, i.e., numbers from 0 to 9999. Idea is to create arrays that store individual parts of output strings. One array is used for single digits, one for numbers from 10 to 19, one for 20, 30, 40, 50, .. etc, and one for powers of 10.
The given number is divided into two parts: the first two digits and the last two digits, and the two parts are printed separately.
C++
// CPP program to print a given number in words. The program // handles numbers from 0 to 9999 #include <bits/stdc++.h> using namespace std; // A function that prints given number in words void convert_to_words( char * num) { int len = strlen ( num); // Get number of digits in given number // Base cases if (len == 0) { fprintf (stderr, "empty string\n" ); return ; } if (len > 4) { fprintf (stderr, "Length more than 4 is not supported\n" ); return ; } /* The first string is not used, it is to make array indexing simple */ char * single_digits[] = { "zero" , "one" , "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine" }; /* The first string is not used, it is to make array indexing simple */ char * two_digits[] = { "" , "ten" , "eleven" , "twelve" , "thirteen" , "fourteen" , "fifteen" , "sixteen" , "seventeen" , "eighteen" , "nineteen" }; /* The first two string are not used, they are to make array indexing simple*/ char * tens_multiple[] = { "" , "" , "twenty" , "thirty" , "forty" , "fifty" , "sixty" , "seventy" , "eighty" , "ninety" }; char * tens_power[] = { "hundred" , "thousand" }; // Used for debugging purpose only cout << "\n" << num << ": " ; // For single digit number if (len == 1) { cout << single_digits[*num - '0' ] << "\n" ; return ; } // Iterate while num is not '\0' while (*num != '\0' ) { // Code path for first 2 digits if (len >= 3) { if (*num - '0' != 0) { cout << single_digits[*num - '0' ] << " " ; cout << tens_power[len - 3] << " " ; // here len can be 3 or 4 } --len; } // Code path for last 2 digits else { /* Need to explicitly handle 10-19. Sum of the two digits is used as index of "two_digits" array of strings */ if (*num == '1' ) { int sum = *num - '0' + *(num + 1) - '0' ; cout << two_digits[sum] << "\n" ; return ; } // Need to explicitly handle 20 else if (*num == '2' && *(num + 1) == '0' ) { cout << "twenty\n" ; return ; } // Rest of the two digit numbers i.e., 21 to 99 else { int i = *num - '0' ; string tm = i ? tens_multiple[i] : "" ; cout << tm << " " ; ++num; if (*num != '0' ) cout << single_digits[*num - '0' ] << " " ; } } ++num; } } // Driver program to test above function int main() { convert_to_words( "9923" ); convert_to_words( "523" ); convert_to_words( "89" ); convert_to_words( "8" ); return 0; } // This code is contributed by Susobhan Akhuli |
C
/* C program to print a given number in words. The program handles numbers from 0 to 9999 */ #include <stdio.h> #include <stdlib.h> #include <string.h> /* A function that prints given number in words */ void convert_to_words( char * num) { int len = strlen ( num); // Get number of digits in given number /* Base cases */ if (len == 0) { fprintf (stderr, "empty string\n" ); return ; } if (len > 4) { fprintf (stderr, "Length more than 4 is not supported\n" ); return ; } /* The first string is not used, it is to make array indexing simple */ char * single_digits[] = { "zero" , "one" , "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine" }; /* The first string is not used, it is to make array indexing simple */ char * two_digits[] = { "" , "ten" , "eleven" , "twelve" , "thirteen" , "fourteen" , "fifteen" , "sixteen" , "seventeen" , "eighteen" , "nineteen" }; /* The first two string are not used, they are to make array indexing simple*/ char * tens_multiple[] = { "" , "" , "twenty" , "thirty" , "forty" , "fifty" , "sixty" , "seventy" , "eighty" , "ninety" }; char * tens_power[] = { "hundred" , "thousand" }; /* Used for debugging purpose only */ printf ( "\n%s: " , num); /* For single digit number */ if (len == 1) { printf ( "%s\n" , single_digits[*num - '0' ]); return ; } /* Iterate while num is not '\0' */ while (*num != '\0' ) { /* Code path for first 2 digits */ if (len >= 3) { if (*num - '0' != 0) { printf ( "%s " , single_digits[*num - '0' ]); printf ( "%s " , tens_power[len - 3]); // here len can // be 3 or 4 } --len; } /* Code path for last 2 digits */ else { /* Need to explicitly handle 10-19. Sum of the two digits is used as index of "two_digits" array of strings */ if (*num == '1' ) { int sum = *num - '0' + *(num + 1) - '0' ; printf ( "%s\n" , two_digits[sum]); return ; } /* Need to explicitly handle 20 */ else if (*num == '2' && *(num + 1) == '0' ) { printf ( "twenty\n" ); return ; } /* Rest of the two digit numbers i.e., 21 to 99 */ else { int i = *num - '0' ; printf ( "%s " , i ? tens_multiple[i] : "" ); ++num; if (*num != '0' ) printf ( "%s " , single_digits[*num - '0' ]); } } ++num; } } /* Driver program to test above function */ int main( void ) { convert_to_words( "9923" ); convert_to_words( "523" ); convert_to_words( "89" ); convert_to_words( "8" ); return 0; } |
Java
// Java program to print a given number in words. The // program handles numbers from 0 to 9999 class GFG { // A function that prints // given number in words static void convert_to_words( char [] num) { // Get number of digits // in given number int len = num.length; // Base cases if (len == 0 ) { System.out.println( "empty string" ); return ; } if (len > 4 ) { System.out.println( "Length more than 4 is not supported" ); return ; } /* The first string is not used, it is to make array indexing simple */ String[] single_digits = new String[] { "zero" , "one" , "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine" }; /* The first string is not used, it is to make array indexing simple */ String[] two_digits = new String[] { "" , "ten" , "eleven" , "twelve" , "thirteen" , "fourteen" , "fifteen" , "sixteen" , "seventeen" , "eighteen" , "nineteen" }; /* The first two string are not used, they are to * make array indexing simple*/ String[] tens_multiple = new String[] { "" , "" , "twenty" , "thirty" , "forty" , "fifty" , "sixty" , "seventy" , "eighty" , "ninety" }; String[] tens_power = new String[] { "hundred" , "thousand" }; /* Used for debugging purpose only */ System.out.print(String.valueOf(num) + ": " ); /* For single digit number */ if (len == 1 ) { System.out.println(single_digits[num[ 0 ] - '0' ]); return ; } /* Iterate while num is not '\0' */ int x = 0 ; while (x < num.length) { /* Code path for first 2 digits */ if (len >= 3 ) { if (num[x] - '0' != 0 ) { System.out.print( single_digits[num[x] - '0' ] + " " ); System.out.print(tens_power[len - 3 ] + " " ); // here len can be 3 or 4 } --len; } /* Code path for last 2 digits */ else { /* Need to explicitly handle 10-19. Sum of the two digits is used as index of "two_digits" array of strings */ if (num[x] - '0' == 1 ) { int sum = num[x] - '0' + num[x + 1 ] - '0' ; System.out.println(two_digits[sum]); return ; } /* Need to explicitly handle 20 */ else if (num[x] - '0' == 2 && num[x + 1 ] - '0' == 0 ) { System.out.println( "twenty" ); return ; } /* Rest of the two digit numbers i.e., 21 to 99 */ else { int i = (num[x] - '0' ); if (i > 0 ) System.out.print(tens_multiple[i] + " " ); else System.out.print( "" ); ++x; if (num[x] - '0' != 0 ) System.out.println( single_digits[num[x] - '0' ]); } } ++x; } } // Driver Code public static void main(String[] args) { convert_to_words( "9923" .toCharArray()); convert_to_words( "523" .toCharArray()); convert_to_words( "89" .toCharArray()); convert_to_words( "8" .toCharArray()); } } // This code is contributed // by Mithun Kumar |
Python3
# Python program to print a given number in # words. The program handles numbers # from 0 to 9999 # A function that prints # given number in words def convert_to_words(num): # Get number of digits # in given number l = len (num) # Base cases if (l = = 0 ): print ( "empty string" ) return if (l > 4 ): print ( "Length more than 4 is not supported" ) return # The first string is not used, # it is to make array indexing simple single_digits = [ "zero" , "one" , "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine" ] # The first string is not used, # it is to make array indexing simple two_digits = [" ", " ten ", " eleven ", " twelve", "thirteen" , "fourteen" , "fifteen" , "sixteen" , "seventeen" , "eighteen" , "nineteen" ] # The first two string are not used, # they are to make array indexing simple tens_multiple = [" ", " ", " twenty ", " thirty ", " forty", "fifty" , "sixty" , "seventy" , "eighty" , "ninety" ] tens_power = [ "hundred" , "thousand" ] # Used for debugging purpose only print (num, ":" , end = " " ) # For single digit number if (l = = 1 ): print (single_digits[ ord (num[ 0 ]) - 48 ]) return # Iterate while num is not '\0' x = 0 while (x < len (num)): # Code path for first 2 digits if (l > = 3 ): if ( ord (num[x]) - 48 ! = 0 ): print (single_digits[ ord (num[x]) - 48 ], end = " " ) print (tens_power[l - 3 ], end = " " ) # here len can be 3 or 4 l - = 1 # Code path for last 2 digits else : # Need to explicitly handle # 10-19. Sum of the two digits # is used as index of "two_digits" # array of strings if ( ord (num[x]) - 48 = = 1 ): sum = ( ord (num[x]) - 48 + ord (num[x + 1 ]) - 48 ) print (two_digits[ sum ]) return # Need to explicitly handle 20 elif ( ord (num[x]) - 48 = = 2 and ord (num[x + 1 ]) - 48 = = 0 ): print ( "twenty" ) return # Rest of the two digit # numbers i.e., 21 to 99 else : i = ord (num[x]) - 48 if (i > 0 ): print (tens_multiple[i], end = " " ) else : print (" ", end=" ") x + = 1 if ( ord (num[x]) - 48 ! = 0 ): print (single_digits[ ord (num[x]) - 48 ]) x + = 1 # Driver Code convert_to_words( "9923" ) # Four Digits convert_to_words( "523" ) # Three Digits convert_to_words( "89" ) # Two Digits convert_to_words( "8" ) # One Digits # This code is contributed # by Mithun Kumar |
C#
// C# program to print a given // number in words. The program // handles numbers from 0 to 9999 using System; class GFG { // A function that prints // given number in words static void convert_to_words( char [] num) { // Get number of digits // in given number int len = num.Length; // Base cases if (len == 0) { Console.WriteLine( "empty string" ); return ; } if (len > 4) { Console.WriteLine( "Length more than " + "4 is not supported" ); return ; } /* The first string is not used, it is to make array indexing simple */ string [] single_digits = new string [] { "zero" , "one" , "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine" }; /* The first string is not used, it is to make array indexing simple */ string [] two_digits = new string [] { "" , "ten" , "eleven" , "twelve" , "thirteen" , "fourteen" , "fifteen" , "sixteen" , "seventeen" , "eighteen" , "nineteen" }; /* The first two string are not used, they are to make array indexing simple*/ string [] tens_multiple = new string [] { "" , "" , "twenty" , "thirty" , "forty" , "fifty" , "sixty" , "seventy" , "eighty" , "ninety" }; string [] tens_power = new string [] { "hundred" , "thousand" }; /* Used for debugging purpose only */ Console.Write(( new string (num)) + ": " ); /* For single digit number */ if (len == 1) { Console.WriteLine(single_digits[num[0] - '0' ]); return ; } /* Iterate while num is not '\0' */ int x = 0; while (x < num.Length) { /* Code path for first 2 digits */ if (len >= 3) { if (num[x] - '0' != 0) { Console.Write( single_digits[num[x] - '0' ] + " " ); Console.Write(tens_power[len - 3] + " " ); // here len can be 3 or 4 } --len; } /* Code path for last 2 digits */ else { /* Need to explicitly handle 10-19. Sum of the two digits is used as index of "two_digits" array of strings */ if (num[x] - '0' == 1) { int sum = num[x] - '0' + num[x + 1] - '0' ; Console.WriteLine(two_digits[sum]); return ; } /* Need to explicitly handle 20 */ else if (num[x] - '0' == 2 && num[x + 1] - '0' == 0) { Console.WriteLine( "twenty" ); return ; } /* Rest of the two digit numbers i.e., 21 to 99 */ else { int i = (num[x] - '0' ); if (i > 0) Console.Write(tens_multiple[i] + " " ); else Console.Write( "" ); ++x; if (num[x] - '0' != 0) Console.WriteLine( single_digits[num[x] - '0' ]); } } ++x; } } // Driver Code public static void Main() { convert_to_words( "9923" .ToCharArray()); convert_to_words( "523" .ToCharArray()); convert_to_words( "89" .ToCharArray()); convert_to_words( "8" .ToCharArray()); } } // This code is contributed // by Mits |
PHP
<?php // PHP program to print a given // number in words. The // program handles numbers // from 0 to 9999 // A function that prints // given number in words function convert_to_words( $num ) { // Get number of digits // in given number $len = strlen ( $num ); // Base cases if ( $len == 0) { echo "empty string\n" ; return ; } if ( $len > 4) { echo "Length more than 4 " . "is not supported\n" ; return ; } /* The first string is not used, it is to make array indexing simple */ $single_digits = array ( "zero" , "one" , "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine" ); /* The first string is not used, it is to make array indexing simple */ $two_digits = array ( "" , "ten" , "eleven" , "twelve" , "thirteen" , "fourteen" , "fifteen" , "sixteen" , "seventeen" , "eighteen" , "nineteen" ); /* The first two string are not used, they are to make array indexing simple*/ $tens_multiple = array ( "" , "" , "twenty" , "thirty" , "forty" , "fifty" , "sixty" , "seventy" , "eighty" , "ninety" ); $tens_power = array ( "hundred" , "thousand" ); /* Used for debugging purpose only */ echo $num . ": " ; /* For single digit number */ if ( $len == 1) { echo $single_digits [ $num [0] - '0' ] . " \n" ; return ; } /* Iterate while num is not '\0' */ $x = 0; while ( $x < strlen ( $num )) { /* Code path for first 2 digits */ if ( $len >= 3) { if ( $num [ $x ]- '0' != 0) { echo $single_digits [ $num [ $x ] - '0' ] . " " ; echo $tens_power [ $len - 3] . " " ; // here len can be 3 or 4 } -- $len ; } /* Code path for last 2 digits */ else { /* Need to explicitly handle 10-19. Sum of the two digits is used as index of "two_digits" array of strings */ if ( $num [ $x ] - '0' == 1) { $sum = $num [ $x ] - '0' + $num [ $x ] - '0' ; echo $two_digits [ $sum ] . " \n" ; return ; } /* Need to explicitly handle 20 */ else if ( $num [ $x ] - '0' == 2 && $num [ $x + 1] - '0' == 0) { echo "twenty\n" ; return ; } /* Rest of the two digit numbers i.e., 21 to 99 */ else { $i = $num [ $x ] - '0' ; if ( $i > 0) echo $tens_multiple [ $i ] . " " ; else echo "" ; ++ $x ; if ( $num [ $x ] - '0' != 0) echo $single_digits [ $num [ $x ] - '0' ] . " \n" ; } } ++ $x ; } } // Driver Code convert_to_words( "9923" ); convert_to_words( "523" ); convert_to_words( "89" ); convert_to_words( "8" ); // This code is contributed // by Mithun Kumar ?> |
Javascript
<script> // JavaScript program to document.write a given number in // words. The program handles numbers // from 0 to 9999 // A function that document.writes // given number in words function convert_to_words(num){ // Get number of digits // in given number let l = num.length // Base cases if (l == 0){ document.write( "empty string" , "</br>" ) return } if (l > 4){ document.write( "Length more than 4 is not supported" , "</br>" ) return } // The first string is not used, // it is to make array indexing simple let single_digits = [ "zero" , "one" , "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine" ] // The first string is not used, // it is to make array indexing simple let two_digits = [ "" , "ten" , "eleven" , "twelve" , "thirteen" , "fourteen" , "fifteen" , "sixteen" , "seventeen" , "eighteen" , "nineteen" ] // The first two string are not used, // they are to make array indexing simple let tens_multiple = [ "" , "" , "twenty" , "thirty" , "forty" , "fifty" , "sixty" , "seventy" , "eighty" , "ninety" ] let tens_power = [ "hundred" , "thousand" ] // Used for debugging purpose only document.write(num, ":" , " " ) // For single digit number if (l == 1){ document.write(single_digits[num.charCodeAt(0) - 48], "</br>" ) return } // Iterate while num is not '\0' let x = 0 while (x < num.length){ // Code path for first 2 digits if (l >= 3){ if (num.charCodeAt(x) - 48 != 0){ document.write(single_digits[num.charCodeAt(x) - 48], " " ) document.write(tens_power[l - 3], " " ) // here len can be 3 or 4 } l -= 1 } // Code path for last 2 digits else { // Need to explicitly handle // 10-19. Sum of the two digits // is used as index of "two_digits" // array of strings if (num.charCodeAt(x) - 48 == 1){ sum = (num.charCodeAt(x) - 48 + num.charCodeAt(x+1) - 48) document.write(two_digits[sum], "</br>" ) return } // Need to explicitly handle 20 else if (num.charCodeAt(x) - 48 == 2 && num.charCodeAt(x + 1) - 48 == 0){ document.write( "twenty" , "</br>" ) return } // Rest of the two digit // numbers i.e., 21 to 99 else { i = num.charCodeAt(x) - 48 if (i > 0) document.write(tens_multiple[i], end= " " ) else document.write( "" , end= "" ) x += 1 if (num.charCodeAt(x) - 48 != 0) document.write(single_digits[num.charCodeAt(x) - 48], "</br>" ) } } x += 1 } } // Driver Code convert_to_words( "9923" ) // Four Digits convert_to_words( "523" ) // Three Digits convert_to_words( "89" ) // Two Digits convert_to_words( "8" ) // One Digits // This code is contributed by shinjanpatra </script> |
9923: nine thousand nine hundred twenty three 523: five hundred twenty three 89: eighty nine 8: eight
Time Complexity: O(N) [N is the length of the string]
Auxiliary Space: O(1)
This article is compiled by Narendra Kangralkar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Approach 2: The following code supports numbers up to 15 digits, i.e., numbers from 0 to trillions. The code prints according to the western number system.
Below is the implementation:
C++
#include <bits/stdc++.h> using namespace std; string numberToWords( long long int n) { long long int limit = 1000000000000, curr_hun, t = 0; // If zero return zero if (n == 0) return ( "Zero" ); // Array to store the powers of 10 string multiplier[] = { "" , "Trillion" , "Billion" , "Million" , "Thousand" }; // Array to store numbers till 20 string first_twenty[] = { "" , "One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine" , "Ten" , "Eleven" , "Twelve" , "Thirteen" , "Fourteen" , "Fifteen" , "Sixteen" , "Seventeen" , "Eighteen" , "Nineteen" }; // Array to store multiples of ten string tens[] = { "" , "Twenty" , "Thirty" , "Forty" , "Fifty" , "Sixty" , "Seventy" , "Eighty" , "Ninety" }; // If number is less than 20, return without any further // computation if (n < 20) return (first_twenty[n]); // Answer variable to store the conversion string answer = "" ; for ( long long int i = n; i > 0; i %= limit, limit /= 1000) { // Store the value in multiplier[t], i.e n = // 1000000, then r = 1, for multiplier(million), 0 // for multipliers(trillion and billion) curr_hun = i / limit; // It might be possible that the current multiplier // is bigger than your number while (curr_hun == 0) { // Set i as the remainder obtained when n was // divided by the limit i %= limit; // Divide the limit by 1000, shifts the // multiplier limit /= 1000; // Get the current value in hundreds, as // English system works in hundreds curr_hun = i / limit; // Shift the multiplier ++t; } // If current hundred is greater than 99, Add the // hundreds' place if (curr_hun > 99) answer += (first_twenty[curr_hun / 100] + " Hundred " ); // Bring the current hundred to tens curr_hun = curr_hun % 100; // If the value in tens belongs to [1,19], add using // the first_twenty if (curr_hun > 0 && curr_hun < 20) answer += (first_twenty[curr_hun] + " " ); // If curr_hun is now a multiple of 10, but not 0 // Add the tens' value using the tens array else if (curr_hun % 10 == 0 && curr_hun != 0) answer += (tens[curr_hun / 10 - 1] + " " ); // If the value belongs to [21,99], excluding the // multiples of 10 Get the ten's place and one's // place, and print using the first_twenty array else if (curr_hun > 20 && curr_hun < 100) answer += (tens[curr_hun / 10 - 1] + " " + first_twenty[curr_hun % 10] + " " ); // If Multiplier has not become less than 1000, // shift it if (t < 4) answer += (multiplier[++t] + " " ); } return (answer); } int main() { long long int n = 36; cout << numberToWords(n) << '\n' ; n = 123456789; cout << numberToWords(n) << '\n' ; n = 10101010110001; cout << numberToWords(n) << '\n' ; n = 999999999; cout << numberToWords(n) << '\n' ; return 0; } // This Code is contributed by Alok Khansali |
Java
public class GFG { static String numberToWords( long n) { long limit = 1000000000000L, curr_hun, t = 0 ; // If zero return zero if (n == 0 ) return ( "Zero" ); // Array to store the powers of 10 String multiplier[] = { "" , "Trillion" , "Billion" , "Million" , "Thousand" }; // Array to store numbers till 20 String first_twenty[] = { "" , "One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine" , "Ten" , "Eleven" , "Twelve" , "Thirteen" , "Fourteen" , "Fifteen" , "Sixteen" , "Seventeen" , "Eighteen" , "Nineteen" }; // Array to store multiples of ten String tens[] = { "" , "Twenty" , "Thirty" , "Forty" , "Fifty" , "Sixty" , "Seventy" , "Eighty" , "Ninety" }; // If number is less than 20, return without any // further computation if (n < 20L) return (first_twenty[( int )n]); String answer = "" ; for ( long i = n; i > 0 ; i %= limit, limit /= 1000 ) { // Store the value in multiplier[t], i.e n = // 1000000, then r = 1, for multiplier(million), // 0 for multipliers(trillion and billion) // multiplier here refers to the current // accessible limit curr_hun = i / limit; // It might be possible that the current // multiplier is bigger than your number while (curr_hun == 0 ) { // Set i as the remainder obtained when n // was divided by the limit i %= limit; // Divide the limit by 1000, shifts the // multiplier limit /= 1000 ; // Get the current value in hundreds, as // English system works in hundreds curr_hun = i / limit; // Shift the multiplier ++t; } // If current hundred is greater than 99, Add // the hundreds' place if (curr_hun > 99 ) answer += (first_twenty[( int )curr_hun / 100 ] + " Hundred " ); // Bring the current hundred to tens curr_hun = curr_hun % 100 ; // If the value in tens belongs to [1,19], add // using the first_twenty if (curr_hun > 0 && curr_hun < 20 ) answer += (first_twenty[( int )curr_hun] + " " ); // If curr_hun is now a multiple of 10, but not // 0 Add the tens' value using the tens array else if (curr_hun % 10 == 0 && curr_hun != 0 ) answer += (tens[( int )curr_hun / 10 - 1 ] + " " ); // If the value belongs to [21,99], excluding // the multiples of 10 Get the ten's place and // one's place, and print using the first_twenty // array else if (curr_hun > 20 && curr_hun < 100 ) answer += (tens[( int )curr_hun / 10 - 1 ] + " " + first_twenty[( int )curr_hun % 10 ] + " " ); // If Multiplier has not become less than 1000, // shift it if (t < 4 ) answer += (multiplier[( int )++t] + " " ); } return (answer); } public static void main(String args[]) { long n = 36L; System.out.println(numberToWords(n)); n = 123456789 ; System.out.println(numberToWords(n)); n = 10101010110001L; System.out.println(numberToWords(n)); n = 999999999 ; System.out.println(numberToWords(n)); } } // This Code is contributed by Alok Khansali |
Python
def numberToWords(n): limit, t = 1000000000000 , 0 # If zero print zero if (n = = 0 ): print ( "zero" ) return # Array to store the powers of 10 multiplier = [" ", " Trillion ", " Billion ", " Million ", " Thousand"] # Array to store numbers till 20 first_twenty = [" ", " One ", " Two", "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine" , "Ten" , "Eleven" , "Twelve" , "Thirteen" , "Fourteen" , "Fifteen" , "Sixteen" , "Seventeen" , "Eighteen" , "Nineteen" ] # Array to store multiples of ten tens = [" ", " Twenty ", " Thirty ", " Forty ", " Fifty", "Sixty" , "Seventy" , "Eighty" , "Ninety" ] # If number is less than 20, print without any if (n < 20 ): print (first_twenty[n]) return answer = "" i = n while (i > 0 ): ''' Store the value in multiplier[t], i.e n = 1000000, then r = 1, for multiplier(million), 0 for multipliers(trillion and billion) multiplier here refers to the current accessible limit ''' curr_hun = i / / limit # It might be possible that the current multiplier is bigger than your number while (curr_hun = = 0 ): # Set i as the remainder obtained when n was divided by the limit i % = limit # Divide the limit by 1000, shifts the multiplier limit / = 1000 # Get the current value in hundreds, as English system works in hundreds curr_hun = i / / limit # Shift the multiplier t + = 1 # If current hundred is greater than 99, Add the hundreds' place if (curr_hun > 99 ): answer + = (first_twenty[curr_hun / / 100 ] + " tensundred " ) # Bring the current hundred to tens curr_hun = curr_hun % 100 # If the value in tens belongs to [1,19], add using the first_twenty if (curr_hun > 0 and curr_hun < 20 ): answer + = (first_twenty[curr_hun] + " " ) # If curr_hun is now a multiple of 10, but not 0 # Add the tens' value using the tens array elif (curr_hun % 10 = = 0 and curr_hun ! = 0 ): answer + = (tens[(curr_hun / / 10 ) - 1 ] + " " ) # If the value belongs to [21,99], excluding the multiples of 10 # Get the ten's place and one's place, and print using the first_twenty array elif (curr_hun > 19 and curr_hun < 100 ): answer + = (tens[(curr_hun / / 10 ) - 1 ] + " " + first_twenty[curr_hun % 10 ] + " " ) # If Multiplier has not become less than 1000, shift it if (t < 4 ): answer + = (multiplier[t] + " " ) i = i % limit limit = limit / / 1000 print (answer) n = 36 numberToWords(n) n = 123456789 numberToWords(n) n = 10101010110001 numberToWords(n) n = 999999999 numberToWords(n) # This code is contributed by Alok Khansali |
C#
// Include namespace system using System; public class numtowords { public static String numberToWords( long n) { var limit = 1000000000000L; long curr_hun; var t = 0; // If zero return zero if (n == 0) { return ( "Zero" ); } // Array to store the powers of 10 String[] multiplier = { "" , "Trillion" , "Billion" , "Million" , "Thousand" }; // Array to store numbers till 20 String[] first_twenty = { "" , "One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine" , "Ten" , "Eleven" , "Twelve" , "Thirteen" , "Fourteen" , "Fifteen" , "Sixteen" , "Seventeen" , "Eighteen" , "Nineteen" }; // Array to store multiples of ten String[] tens = { "" , "Twenty" , "Thirty" , "Forty" , "Fifty" , "Sixty" , "Seventy" , "Eighty" , "Ninety" }; // If number is less than 20, return without any // further computation if (n < 20L) { return (first_twenty[( int )n]); } var answer = "" ; for ( long i = n; i > 0; i %= limit, limit /= 1000) { // Store the value in multiplier[t], i.e n = // 1000000, then r = 1, for multiplier(million), // 0 for multipliers(trillion and billion) // multiplier here refers to the current // accessible limit curr_hun = i / limit; // It might be possible that the current // multiplier is bigger than your number while (curr_hun == 0) { // Set i as the remainder obtained when n // was divided by the limit i %= limit; // Divide the limit by 1000, shifts the // multiplier limit /= 1000; // Get the current value in hundreds, as // English system works in hundreds curr_hun = i / limit; // Shift the multiplier ++t; } // If current hundred is greater than 99, Add // the hundreds' place if (curr_hun > 99) { answer += (first_twenty[( int )(( int )curr_hun / 100)] + " Hundred " ); } // Bring the current hundred to tens curr_hun = curr_hun % 100; // If the value in tens belongs to [1,19], add // using the first_twenty if (curr_hun > 0 && curr_hun < 20) { answer += (first_twenty[( int )curr_hun] + " " ); } else if (curr_hun % 10 == 0 && curr_hun != 0) { answer += (tens[( int )(( int )curr_hun / 10) - 1] + " " ); } else if (curr_hun > 20 && curr_hun < 100) { answer += (tens[( int )(( int )curr_hun / 10) - 1] + " " + first_twenty[( int )curr_hun % 10] + " " ); } // If Multiplier has not become less than 1000, // shift it if (t < 4) { answer += (multiplier[( int )++t] + " " ); } } return (answer); } public static void Main(String[] args) { var n = 36L; Console.WriteLine(numtowords.numberToWords(n)); n = 123456789; Console.WriteLine(numtowords.numberToWords(n)); n = 10101010110001L; Console.WriteLine(numtowords.numberToWords(n)); n = 999999999; Console.WriteLine(numtowords.numberToWords(n)); } } |
Javascript
function numberToWords(n) { let limit = 1000000000000, t = 0 // If zero console.log zero if (n == 0) { console.log( "zero" ) return } // Array to store the powers of 10 let multiplier = [ "" , "Trillion" , "Billion" , "Million" , "Thousand" ] // Array to store numbers till 20 let first_twenty = [ "" , "One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine" , "Ten" , "Eleven" , "Twelve" , "Thirteen" , "Fourteen" , "Fifteen" , "Sixteen" , "Seventeen" , "Eighteen" , "Nineteen" ] // Array to store multiples of ten let tens = [ "" , "Twenty" , "Thirty" , "Forty" , "Fifty" , "Sixty" , "Seventy" , "Eighty" , "Ninety" ] // If number is less than 20, console.log without any if (n < 20) { console.log(first_twenty[n]) return } let answer = "" let i = n while (i > 0) { /* Store the value in multiplier[t], i.e n = 1000000, then r = 1, for multiplier(million), 0 for multipliers(trillion and billion) multiplier here refers to the current accessible limit */ let curr_hun = Math.floor(i / limit) // It might be possible that the current multiplier is bigger than your number while (curr_hun == 0) { // Set i as the remainder obtained when n was divided by the limit i %= limit // Divide the limit by 1000, shifts the multiplier limit /= 1000 // Get the current value in hundreds, as English system works in hundreds curr_hun = Math.floor(i / limit) // Shift the multiplier t += 1 } let flr = Math.floor(curr_hun / 100); // If current hundred is greater than 99, Add the hundreds' place if (curr_hun > 99) answer += (first_twenty[flr] + " tensundred " ) // Bring the current hundred to tens curr_hun = curr_hun % 100 // If the value in tens belongs to [1,19], add using the first_twenty if (curr_hun > 0 && curr_hun < 20) answer += (first_twenty[curr_hun] + " " ) // If curr_hun is now a multiple of 10, but not 0 // Add the tens' value using the tens array else if (curr_hun % 10 == 0 && curr_hun != 0){ flr = Math.floor(curr_hun / 10); answer += (tens[flr - 1] + " " ) } // If the value belongs to [21,99], excluding the multiples of 10 // Get the ten's place and one's place, and console.log using the first_twenty array else if (curr_hun > 19 && curr_hun < 100){ flr = Math.floor(curr_hun / 10); answer += (tens[flr - 1] + " " + first_twenty[curr_hun % 10] + " " ) } // If Multiplier has not become less than 1000, shift it if (t < 4) answer += (multiplier[t] + " " ) i = i % limit limit = Math.floor(limit / 1000) } console.log(answer) } let n = 36 numberToWords(n) n = 123456789 numberToWords(n) n = 10101010110001 numberToWords(n) n = 999999999 numberToWords(n) // This code is contributed by phasing17. |
Thirty Six One Hundred Twenty Three Million Four Hundred Fifty Six Thousand Seven Hundred Eighty Nine Ten Trillion One Hundred One Billion Ten Million One Hundred Ten Thousand One Nine Hundred Ninety Nine Million Nine Hundred Ninety Nine Thousand Nine Hundred Ninety Nine
Time Complexity: O(Log10(N))
Auxiliary Space: O(1)
Please Login to comment...