Given an integer N, your task is to convert the given number into words using PHP.
Examples:
Input: N = 958237764 Output: Nine Hundred Fifty Eight Million Two Hundred Thirty Seven Thousand Seven Hundred Sixty Four
Input: N = 5000 Output: Five Thousand Below are the approaches to convert a given number to words:
Using Array In this method we use arrays and loops to convert numbers to words. It processes the given number by breaking it down into its individual digits and constructs the word representation by concatenating the corresponding words from the arrays.
Example: Below is the implementation of the above approach to convert a given number to words.
PHP
<?php
function convert_to_words_array($number)
{
$words = array(
'0' => 'Zero', '1' => 'One', '2' => 'Two',
'3' => 'Three', '4' => 'Four', '5' => 'Five',
'6' => 'Six', '7' => 'Seven', '8' => 'Eight',
'9' => 'Nine', '10' => 'Ten', '11' => 'Eleven',
'12' => 'Twelve', '13' => 'Thirteen', '14' => 'Fourteen',
'15' => 'Fifteen', '16' => 'Sixteen', '17' => 'Seventeen',
'18' => 'Eighteen', '19' => 'Nineteen', '20' => 'Twenty',
'30' => 'Thirty', '40' => 'Forty', '50' => 'Fifty', '60' => 'Sixty',
'70' => 'Seventy', '80' => 'Eighty', '90' => 'Ninety'
);
if ($number <= 20) {
return $words[$number];
}
elseif ($number < 100) {
return $words[10 * floor($number / 10)]
. ($number % 10 > 0 ? ' ' . $words[$number % 10] : '');
}
else {
$output = '';
if ($number >= 1000000000) {
$output .= convert_to_words_array(floor($number / 1000000000))
. ' Billion ';
$number %= 1000000000;
}
if ($number >= 1000000) {
$output .= convert_to_words_array(floor($number / 1000000))
. ' Million ';
$number %= 1000000;
}
if ($number >= 1000) {
$output .= convert_to_words_array(floor($number / 1000))
. ' Thousand ';
$number %= 1000;
}
if ($number >= 100) {
$output .= convert_to_words_array(floor($number / 100))
. ' Hundred ';
$number %= 100;
}
if ($number > 0) {
$output .= ($number <= 20) ? $words[$number] :
$words[10 * floor($number / 10)] . ' '
. ($number % 10 > 0 ? $words[$number % 10] : '');
}
return trim($output);
}
}
echo convert_to_words_array(958237764);
?>
OutputNine Hundred Fifty Eight Million Two Hundred Thirty Seven Thousand Seven Hundred Sixty Four Time Complexity: O(N), where N is the number of digits in the number
Auxiliary Space: O(1)
Using Iterative ApproachIn this approach we use an iterative approach without recursion. It handles the number by breaking it down into segments (billions, millions, thousands, and hundreds) and converting each segment to words.
Example: Below is the implementation of the above approach to convert a given number to words
PHP
<?php
function numberToWords($num)
{
if ($num == 0) {
return "Zero";
}
$words = array(
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten",
"Eleven",
"Twelve",
"Thirteen",
"Fourteen",
"Fifteen",
"Sixteen",
"Seventeen",
"Eighteen",
"Nineteen"
);
$tens = array(
"Twenty",
"Thirty",
"Forty",
"Fifty",
"Sixty",
"Seventy",
"Eighty",
"Ninety"
);
$thousands = array(
"",
"Thousand",
"Million",
"Billion"
);
$words_str = "";
$segment = 0;
while ($num > 0) {
$chunk = $num % 1000;
if ($chunk != 0) {
$chunk_words = "";
if ($chunk >= 100) {
$chunk_words .= $words[(int) ($chunk / 100) - 1]
. " Hundred ";
$chunk %= 100;
}
if ($chunk >= 20) {
$chunk_words .= $tens[(int) ($chunk / 10) - 2] . " ";
$chunk %= 10;
}
if ($chunk > 0) {
$chunk_words .= $words[$chunk - 1] . " ";
}
$words_str = trim($chunk_words) . " "
. $thousands[$segment] . " " . $words_str;
}
$num = (int) ($num / 1000);
$segment++;
}
return trim($words_str);
}
$num1 = 958237764;
$num2 = 55000000;
echo "Input: $num1\n";
echo "Output: " . numberToWords($num1) . "\n\n";
echo "Input: $num2\n";
echo "Output: " . numberToWords($num2) . "\n";
OutputInput: 958237764
Output: Nine Hundred Fifty Eight Million Two Hundred Thirty Seven Thousand Seven Hundred Sixty Four
Input: 55000000
Output: Fifty Five Million
Time Complexity: O(N), where N is the number of digits in the number.
Auxiliary Space: O(1)
Using Recursive Function for Large NumbersIn this approach we use recursive function to break down the number into smaller parts, such as hundreds, thousands, or millions, and recursively converting each part into words and combining them.
Example: Below is the implementation of the above approach to convert a given number to words
PHP
<?php
function convert_to_words($number)
{
$words = array(
0 => 'Zero',
1 => 'One',
2 => 'Two',
3 => 'Three',
4 => 'Four',
5 => 'Five',
6 => 'Six',
7 => 'Seven',
8 => 'Eight',
9 => 'Nine',
10 => 'Ten',
11 => 'Eleven',
12 => 'Twelve',
13 => 'Thirteen',
14 => 'Fourteen',
15 => 'Fifteen',
16 => 'Sixteen',
17 => 'Seventeen',
18 => 'Eighteen',
19 => 'Nineteen',
20 => 'Twenty',
30 => 'Thirty',
40 => 'Forty',
50 => 'Fifty',
60 => 'Sixty',
70 => 'Seventy',
80 => 'Eighty',
90 => 'Ninety'
);
if ($number <= 20) {
return $words[$number];
} elseif ($number < 100) {
return $words[10 * floor($number / 10)]
. ($number % 10 > 0 ? ' ' . $words[$number % 10] : '');
} elseif ($number < 1000) {
return $words[floor($number / 100)] .
' Hundred' . ($number % 100 > 0 ? ' and '
. convert_to_words($number % 100) : '');
} elseif ($number < 1000000) {
return convert_to_words(floor($number / 1000))
. ' Thousand' . ($number % 1000 > 0 ? ' '
. convert_to_words($number % 1000) : '');
} elseif ($number < 1000000000) {
return convert_to_words(floor($number / 1000000))
. ' Million' . ($number % 1000000 > 0 ? ' '
. convert_to_words($number % 1000000) : '');
} else {
return convert_to_words(floor($number / 1000000000))
. ' Billion' . ($number % 1000000000 > 0 ? ' '
. convert_to_words($number % 1000000000) : '');
}
}
echo convert_to_words(958237764);
OutputNine Hundred and Fifty Eight Million Two Hundred and Thirty Seven Thousand Seven Hundred and Sixty Four Time Complexity: O(N)
Auxiliary Space: O(N)
|