Table of Contents
Function to divide two numbers:PHP program
In this article, we will discuss the concept of Function to divide two numbers
In this post, we are going to learn how to write a program to find division of two numbers using function in PHP language
Code to find division of two numbers in PHP
Division of two numbers using function – #1
Program 1
- Start PHP script
- Declare two integer variable as parameter
- Calculate division of given two numbers and return the value to main
- Calling the function with passing argument
- Display result on the screen
- Exit PHP
In this program, first the user declare variables as parameter in function. Then when the user call the function, passing the values to variables as argument. finally , the result is calculated and display on the screen
Program 1
<html>
<head>
<title> Divide two numbers</title>
</head>
<body>
<h3>Division of two number using form</h3>
<?php //php script
//function definition with parameter
function divTwoNumbers($x, $y)
{
return $x / $y;
//calculate the product of two numbers
//and return the product of given number
}
//Calling the function with argument to print result
echo "The division of 150 and 3: ".divTwoNumbers(150,3);
echo "<br>";
echo "The division of -75 and -15: ".divTwoNumbers(-75,-15);
echo "<br>";
echo "The division of 120 and -20: ".divTwoNumbers(120,-20);
echo "<br>";
//exit php
?>
</body>
</html>
When the above code is executed, it produces the following result
Division of two number using form
The division of 150 and 3: 50 The division of -75 and -15: 5 The division of 120 and -20: -6
Division of two numbers using function – #2
Program 2
- Start PHP script
- Declare two integer variable as parameter
- Calculate division of given two numbers and return value to main
- Calling the function with argument
- Display result on the screen
- Exit PHP
In this program, first the user declare variables as parameter in function. Then when the user call the function, passing the values to variables as argument. finally , the result is calculated and display on the screen
<html>
<head>
<title> Divide two numbers</title>
</head>
<body>
<h3>Division of two number using form</h3>
<?php //php script
//function definition with parameter
function divTwoNumbers($x, $y)
{
$div=$x / $y;
//calculate division of two numbers
//using division(/) operator
return $div;//return the result to main
echo $div;//print the result when calling the function
}
//Calling the function and print result
echo "The division of 120 and 10: ";
echo divTwoNumbers(120,10);
echo "<br>";
echo "The division of 250 and -5: ";
echo divTwoNumbers(250,-5);
echo "<br>";
echo "The division of -180 and -20: ";
echo divTwoNumbers(-180,-20);
echo "<br>";
//exit php
?>
</body>
</html>
When the above code is executed, it produces the following result
Division of two number using form
The division of 120 and 10: 12 The division of 250 and -5: -50 The division of -180 and -20: 9
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 quotient and remainder
C program to find quotient and remainder
C++ program to find quotient and remainder
Python program to find quotient and remainder