Created
April 4, 2022 05:07
-
-
Save sonus/d6bb3517d89b9e18e1be609ffe5d253d to your computer and use it in GitHub Desktop.
Int To Roman
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Solution { | |
private $romans = [ | |
'M' => 1000, | |
'CM'=> 900, | |
'D'=>500, | |
'CD'=>400, | |
'C'=>100, | |
'XC'=>90, | |
'L'=>50, | |
'XL'=>40, | |
'X'=>10, | |
'IX'=>9, | |
'V'=>5, | |
'IV'=>4, | |
'I'=>1 | |
]; | |
/** | |
* @param Integer $num | |
* @return String | |
*/ | |
function intToRoman($num) { | |
$returnRoman = ""; | |
while($num > 0) { | |
foreach($this->romans as $roman => $value) { | |
if($num >= $value){ | |
$num -= $value; | |
$returnRoman .= $roman; | |
break; | |
} | |
} | |
} | |
return $returnRoman; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment