How to use Recursive Approach In PHP
A recursive approach is also possible for converting an integer to Roman numerals.
PHP
<?php function intToRoman( $num ) { if ( $num <= 0) { return '' ; } $mapping = [ 1000 => 'M' , 900 => 'CM' , 500 => 'D' , 400 => 'CD' , 100 => 'C' , 90 => 'XC' , 50 => 'L' , 40 => 'XL' , 10 => 'X' , 9 => 'IX' , 5 => 'V' , 4 => 'IV' , 1 => 'I' ]; foreach ( $mapping as $value => $roman ) { if ( $num >= $value ) { return $roman . intToRoman( $num - $value ); } } return '' ; } // Driver code $num = 1111; echo "Roman numeral for $num is: " . intToRoman( $num ); ?> |
Output
Roman numeral for 1111 is: MCXI
PHP Program to Convert Integer to Roman Number
Converting an integer to a Roman number is a common problem often encountered in applications dealing with dates, historical data, and numeral representations. In this article, we will explore various approaches to converting an integer to Roman numerals in PHP.
Table of Content
- Using Simple Mapping Array
- Using Recursive Approach