Question

Please make sure to write the test function!

Using PHP, design a function that given a Roman numerale in input, is able to compute the modern Hindu-Arabic numeral system

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts please comment below. Please give upvote if you like this.

Answer :

<?php
function test($nr){
//array created with the priority order.
$romans = array(
'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,
);

$result = 0;
//In the below loop we are searching the input with the roman array if we found a match with the input in the array than related value is added to result.
foreach ($romans as $key => $value) {
while (strpos($nr, $key) === 0) {
$result += $value;
$nr = substr($nr, strlen($key));
}
}
echo $result."<br>";
}
test('VI');
test('IV');
test('MCMXC');
test('IX');

?>

€ → C localhost/HomeworkLib/vinu.php 1990

Add a comment
Know the answer?
Add Answer to:
Please make sure to write the test function! Using PHP, design a function that given a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using PHP: Write a function that: Given a numerical parameter in input, computes and prints all...

    Using PHP: Write a function that: Given a numerical parameter in input, computes and prints all the prime numbers up to that value. Example: If input parameter is 10, output is "2, 3, 5, 7" If input parameter is 100, output is "2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97" Write a "tester function" that: Given a specific subset of inputs, tests the...

  • Exercise #1: Write a C program that contains the following steps (make sure all variables are...

    Exercise #1: Write a C program that contains the following steps (make sure all variables are int). Read carefully each step as they are not only programming steps but also learning topics that explain how functions in C really work. Ask the user for a number between 10 and 99. Write an input validation loop to make sure it is within the prescribed range and ask again if not. Commenting out the existing coding, write the code to divide a...

  • If you’re using Visual Studio Community 2015, as requested, the instructions below should be exact but...

    If you’re using Visual Studio Community 2015, as requested, the instructions below should be exact but minor discrepancies may require you to adjust. If you are attempting this assignment using another version of Visual Studio, you can expect differences in the look, feel, and/or step-by-step instructions below and you’ll have to determine the equivalent actions or operations for your version on your own. INTRODUCTION: In this assignment, you will develop some of the logic for, and then work with, the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT