Table of Contents
PHP program to divide two numbers
In this article, we will discuss the concept of a PHP program to divide two numbers
In this post, we are going to learn how to write a program to calculate the division of two numbers in the PHP language
Code to the division of two numbers in PHP
Find the division of two numbers – #1
Program 1
- Start HTML tag
- Start PHP script
- Declare and initialize two integer variable
- Calculate the division of given two numbers
- Display the result on the screen
- Exit PHP
- Close HTML tag
In this program, the user declares and initializes the values to variables and calculates the division of the given numbers using the division(/) operator. And then the result is displayed on the screen.
Program 1
<html> <head> <title> Division of two numbers</title> </head> <body> <h3>Division of two number using form</h3> <?php //php script $n1=30; $n2=3; $div=$n1/$n2; echo "Division of given numbers: $n1/$n2=".$div; //exit php ?> </body> </html>
When the above code is executed, it produces the following result
Division of two numbers using the form
Division of given numbers: 30/3=10
Find the division of two numbers – #2
In this program, the user declares and initiates the values to variables and calculates the division of the given numbers using the division(/) operator. then the result is assigned to a third variable ($res) and the result is displayed on the screen using the third variable
Program 2
- Start HTML tag
- Start PHP script
- Declare and initialize two integer variable
- Calculate the division of given two numbers
- Assign the result to the third variable
- Display the result on the screen using the third variable
- Exit PHP
- Close HTML tag
<html> <head> <title> Division of two numbers</title> </head> <body> <h3>Division of two number using form</h3> <?php //php script $n1=40; $n2=8; $div=$n1/$n2; $res="Division of given numbers: $n1/$n2=".$div; echo $res; //exit php ?> </body> </html>
When the above code is executed, it produces the following result
Division of two numbers using the form
Division of given numbers: 40/8=5
Find the division of two numbers using form- #3
In this program, the user enters the values to variables using the form(the program Asks input from the user to form) and calculates the division of the given numbers using the division(/) operator and the result is assigned to a third variable ($div) . Finally, the result is displayed on the screen using the third variable.
- Create HTML form
- Exit HTML form
- Start PHP script
- Exit PHP
program 3
<html> <head> <title> Divide two numbers</title> </head> <body> <h3>Division of two number using form</h3> <form method = "post"> Enter 1st number <input type = "number" name="number1"/><br><br> Enter 2nd number <input type = "number" name="number2"/><br><br> <input type = "submit" name="submit" value="Division"/><br> </form> <?php if(isset($_POST['submit'])) { $number1=$_POST['number1']; $number2=$_POST['number2']; $div=$number1 / $number2; echo "The division of $number1 and $number2 is: ".$div; } ?> </body> </html>
When the above code is executed, it produces the following result
Find the division of two numbers using the form- #4
In this program, the user enters the values of variables using the form(the program Asks input from the user to form) and calculates the division of the given numbers using the division(/) operator. Finally, the result is displayed on the screen using the echo function.
- Create HTML form
- Exit HTML form
- Start PHP script
- Exit PHP
Program 4
<html> <head> <title> Divide two numbers</title> </head> <body> <h3>Division of two number using form</h3> <form> Enter 1st number <input type = "number" name="num1"/><br><br> Enter 2nd number <input type = "number" name="num2"/><br><br> <input type = "submit" name="submit" value="division"/><br> </form> <?php if(isset($_GET['num1']) && isset ($_GET['num1'])){ $num1=intval($_GET['num1']); $num2=intval($_GET['num2']); echo "The division of $num1 and $num2 is: "; echo $num1/$num2; } ?> </body> </html>
When the above code is executed, it produces the following result
Suggested post
Java program to divide two number
C program to divide two number
C++ program to divide two number
Python program to divide two number
java program to find the quotient and remainder
C program to find quotient and remainder
C++ program to find quotient and remainder
Python program to find quotient and remainder