Did you know?

Any 2 digit numbers with the same tens place number and the sum of ones place is 10 can be easily multiplied by

Steps:

1. Add + 1 on one of the tens then multiply.
2. For the ones place, just multiply it.

Example

33
37

Same tens which is 3(tens place) and 7+3(ones place) = 10

Tens

Add + 1 on one of the tens then multiply.

3*(3+1) = 12

Ones

Just Multiply

7*3 = 21

so 33*37 = 1221

Another Example

86
84

same tens which is 8 and 6+4 = 10

Tens

Add + 1 on one of the tens then multiply.

8*(8+1) = 72

Ones

Just Multiply

6*4 = 24

so 86*84 = 7224

Design patterns are used for commonly occuring problems. But take note that not everything can be solved with patterns. It’s for you to decide when a pattern is appropriate and when it’s completely useless.
I’m starting to think that converting numbers to words doesn’t need a design pattern. But for the sake of learning, I’m doing it. :)

``````<?php

interface ConvertNumber
{
public function convertToWords(\$number);
}

//ones
\$one = array("", "one ", "two ", "three ", "four ",
"five ", "six ", "seven ", "eight ",
"nine ", "ten ", "eleven ", "twelve ",
"thirteen ", "fourteen ", "fifteen ",
"sixteen ", "seventeen ", "eighteen ",
"nineteen ");

//tens
\$ten = array("", "", "twenty ", "thirty ", "forty ",
"fifty ", "sixty ", "seventy ", "eighty ",
"ninety ");

// number is in ones or tens place
function numberToWords(\$number, \$str)
{
global \$one, \$ten;
\$word = "";

// if number is more than 19, divide it
if (\$number > 19)
{
\$word .= \$ten[(int)(\$number / 10)];
\$word .= \$one[\$number % 10];
}
else
\$word .= \$one[\$number];

// if number is non-zero
if (\$number != 0 )
\$word .= \$str;

return \$word;
}

/**
* Class for converting ones and tens
*/
class ConvertOnesAndTens implements ConvertNumber
{
public function convertToWords(\$number)
{
\$numberInWords = "";

// handles ones and tens digits
\$numberInWords .= numberToWords((\$number % 100), "");

return \$numberInWords;
}
}

/**
* Class for converting hundreds
*/
class ConvertHundreds implements ConvertNumber
{
protected \$convertNumber;

function __construct(ConvertNumber \$convertNumber)
{
\$this->convertNumber = \$convertNumber;
}

public function convertToWords(\$number)
{
\$numberInWords = "";

// handles hundred digits
\$numberInWords .= numberToWords(((int)(\$number / 100) % 10), "hundred ");

return  \$numberInWords . \$this->convertNumber->convertToWords(\$number);
}
}

/**
* Class for converting thousands
*/
class ConvertThousands implements ConvertNumber
{
protected \$convertNumber;

function __construct(ConvertNumber \$convertNumber)
{
\$this->convertNumber = \$convertNumber;
}

public function convertToWords(\$number)
{
\$numberInWords = "";

// handles thousand digits
\$numberInWords .= numberToWords(((int)(\$number / 1000) % 100), "thousand ");

return  \$numberInWords . \$this->convertNumber->convertToWords(\$number);
}
}

/**
* Class for converting hundred thousands
*/
class ConvertHundredThousands implements ConvertNumber
{
protected \$convertNumber;

function __construct(ConvertNumber \$convertNumber)
{
\$this->convertNumber = \$convertNumber;
}

public function convertToWords(\$number)
{
\$numberInWords = "";

// handles hundred thousand digits
\$numberInWords .= numberToWords(((int)(\$number / 100000) % 100), "hundred ");

return  \$numberInWords . \$this->convertNumber->convertToWords(\$number);
}
}

echo (new ConvertHundredThousands(new ConvertThousands(new ConvertHundreds(new ConvertOnesAndTens()))))->convertToWords(253678);

?>
``````