settingsAccountsettings
Menusettings

Q: Multiply and Divide Numbers in PHP

+2 votes

You are given a number num1 and a number num2.

Write a PHP script that:

  • Multiplies num1 * num2 if num2 is greater than or equal to num1.
  • Divides num1 / num2 if num1 is greater than num2.

The input comes as parameters named num1 and num2. Print the output in the HTML page.

Examples:

Parameters names

Input

Output

 

Input

Output

 

Input

Output

num1

2

6

 

13

169

3

1.5

num2

3

 

13

2

 

asked in PHP category by user mitko
edited by user golearnweb

1 Answer

+1 vote
 
Best answer

Here is the code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
<form>
    N: <input type="text" name="num1"/>
    M: <input type="text" name="num2"/>
    <input type="submit"/>
</form>

<?php
if (isset($_GET['num1']) && isset($_GET['num2'])) {
    $num1 = $_GET['num1'];
    $num2 = $_GET['num2'];
    if ($num2 >= $num1) {
        echo $num1 * $num2;
    } else if ($num1 >= $num2) {
        echo $num1 / $num2;
    }
}
?>

</body>
</html>

 

answered by user nikole
selected by user golearnweb
...