Question

In: Computer Science

Utilizing PHP and HTML code Create a form that has text inputs for two numbers and...

Utilizing PHP and HTML code

Create a form that has text inputs for two numbers and a submit button. Submit the values to a program that calculates and displays the GCD of the two numbers. The greatest common divisor of two integers a and b is the largest integer that is a factor of both a and b.

The GCD of any number and 1 is 1, and the GCD of any number and 0 is that number.

For example:
gcd(24, 84) =12,
gcd(105, 45) =15,
gcd(409, 1) = 1,
and gcd(0, 8) =8.

You could set up a loop for each number and find all the factors using the modulo operator. Then you could compare the two lists of factors and find the largest number they have in common. This would work but it is going to be slow for very large numbers.

One efficient way to compute the GCD is to use Euclid's Algorithm, which states the following:

gcd(a, b) = gcd(b, a % b)
gcd(a, 0) = Absolute value of a

Here is an example of using this algorithm for 154,320 and 4,320

  1. Find the larger of the two numbers = 154,320
  2. 154320 % 4320 = 3120
  3. 154320 % 3120 = 1440
  4. 154320 % 1440 = 240
  5. 154320 % 240 = 0 **** the remainder is 0 so the GCD is 240

Looks like it would be perfect for a while loop.

Solutions

Expert Solution

1. Install xampp

2. Make folder in htdocs in xampp in c drive. Save this code with name index.php in that folder.

3.On xampp control pannel open phpMyadmin using apache admin button.

4. open this file in the server

index.php

<?php               //php code starts here

    if(isset($_POST['num1']) && isset($_POST['num2'])) {

        // If isset then assign data to variable $name = $_POST['num1]

        $num1 = $_POST['num1'];      //$_POST is global varible
        $num2 = $_POST['num2'];      //$_POST['num1'] pr $_POST['num2'] grabs the form element with name num1/num2.

        function calcGcd(int $num1, int $num2) {
            if($num1 == 0)
                return $num2;
            return calcGcd($num2 % $num1, $num1);
        }

        echo "GCD of ".$num1." and ".$num2.": ".calcGcd($num1, $num2)."<br><br>";     //calling calcGCD() function


        } else {
            //redirect to error page
            //header('location: index.php');
        }

    //php code ends here
?>          


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="index.php" method="post" name="form1">
        Number_1 <input type="text" name="num1"><br><br>
        Number_2 <input type="text" name="num2"><br><br>
        <input type="submit" name="submit" value="submit">
    </form>
</body>
</html>

Output1:

before entering the numbers

Output 2:

After entering the numbers


Related Solutions

- Create an html form (bank form) and the PHP code with 2 accounts with options...
- Create an html form (bank form) and the PHP code with 2 accounts with options to transfer, deposit or withdraw money.
Please create a PHP action codes for the HTML CODE provided below. <!DOCTYPE html> <html> <head>...
Please create a PHP action codes for the HTML CODE provided below. <!DOCTYPE html> <html> <head> <title> Project for keeping all your input </title> </head> <body> <h1>Welcome to this Web Based Test!!!</h1> <p>Please answer the following questions:</p> <hr/> <form action="" method="post"> Name: <input type="text" name="name" value=""> <font color=red>*</font><br/><br/> E-mail: <input type="text" name="email" value=""> <font color=red>*</font><br> <hr/> Choose your major area of study: <select name="major"> <option value="Digital Media" >Digital Media</option> <option value="Software" >Software</option> <option value="Security" >Security</option> <option value="Business" >Business</option> <option value="Other"...
Create an HTML form with the following: 2 text input fields 1 text area 2 radio...
Create an HTML form with the following: 2 text input fields 1 text area 2 radio buttons or 3 checkboxes 1 button Include labels for fields
Create a student attendance form in php. please provide source code examples
Create a student attendance form in php. please provide source code examples
Use PHP, javascript and a little bit of Html More focus on php Create a logout...
Use PHP, javascript and a little bit of Html More focus on php Create a logout page (for patients) in hotel management. Please provide the screenshot of the output too (logout page). Hint: the logout page should include like this thanks for visting etcetera. Thanks in advance
PHP Question - Subject: PHP File Handling and Uploads INSTRUCTIONS: Objective: • Create a sticky HTML...
PHP Question - Subject: PHP File Handling and Uploads INSTRUCTIONS: Objective: • Create a sticky HTML form. • Submit form for processing. • Sanitize and validate form data. • Upload a profile image. • Add a record to a text file. • Display form results with content from another text file and the uploaded image. Description: This assignment deals with file management. It requires the use of 3 new files and 1 new directory. You will create the new file-uploads.php...
PHP Question - Subject: PHP File Handling and Uploads INSTRUCTIONS: Objective: • Create a sticky HTML...
PHP Question - Subject: PHP File Handling and Uploads INSTRUCTIONS: Objective: • Create a sticky HTML form. • Submit form for processing. • Sanitize and validate form data. • Upload a profile image. • Add a record to a text file. • Display form results with content from another text file and the uploaded image. Description: This assignment deals with file management. It requires the use of 3 new files and 1 new directory. You will create the new file-uploads.php...
Write a PHP program using HTML form. It will take Length, Width and Height of a...
Write a PHP program using HTML form. It will take Length, Width and Height of a box as input. When a button is pressed, it will calculate the Volume. If Volume is less than 25 then display the message “Small box”. If Volume is from 25 to 50, it will display the message “Medium box”. When the Volume is greater than 50, then display the message “Large box”.
Create, test, and validate an HTML document that has a form with the following controls: a....
Create, test, and validate an HTML document that has a form with the following controls: a. A text box to collect the user's name b. Four checkboxes, one for each of the following items: i. Four 25-watt light bulbs for $2.39 ii. Eight 25-watt light bulbs for $4.29 iii. Four 25-watt long-life light bulbs for $3.95 iv. Eight 25-watt long-life light bulbs for $7.49 c. A collection of three radio buttons that are labeled as follows: i. Visa ii. Master...
1. Create a PHP page with standard HTML tags. Remember to save the file with the...
1. Create a PHP page with standard HTML tags. Remember to save the file with the .php extension. Inside the <body> tag, create a PHP section that will show the text "Hello World!" 2. For this exercise, echo the phrase "Twinkle, Twinkle little star." Create two variables, one for the word "Twinkle" and one for the word "star". Echo the statement tothe browser. 3. PHP includes all the standard arithmetic operators. For this PHP exercise, you will use them along...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT