Table of Contents
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
Program 1
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
Program 2
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
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.