Dec 31, 2010

Finding the Roman Value

Finding the Roman Value:

use strict;

print "Enter Decimal Number: ";
chomp(my $a = <stdin>);
print "Roman Value: ". &decimalroman($a);


sub decimalroman
{
   
    my $input = shift;
    print "$input";
    my @roman = ("M", "CM", "D", "CD", "C", "XC", "L","XL", "X", "IX", "V", "IV", "I");
    my @decimal =   (1000, 900, 500,  400, 100, 90,   50,  40,  10,   9,    5,    4,   1);
    my $romanvalue = "";
   
    for(my $i = 0; $i<13; $i++)
    {
       while($input >= $decimal[$i]){
           $input -= $decimal[$i];
           $romanvalue .= $roman[$i];
       }
    }
   
    return $romanvalue;
}

No comments:

Post a Comment