PHP program to divide two numbers using function
- Home
- Calculations
- PHP program to divide two numbers using function
- On
- By
- 0 Comment
- Categories: Calculations, division
PHP program to divide two numbers using function
PHP program to divide two numbers using function
In this tutorial, we will discuss the concept of PHP program to divide two numbers using function
In this topic, we are going to learn how to write a program to division of two numbers using user-defined function in PHP programming language
Calculate division of two numbers
Calculate division of two numbers using function – #1
In this program, the user declare two integer variables $x and $y as parameters of the user defined function. Then two numbers are divided using division(/) operator. finally when the user call the function with passing argument, the result is displayed on the screen
Program 1
<?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 15 and 3: ".divTwoNumbers(15,3); echo "<br>"; echo "The division of -75 and -5: ".divTwoNumbers(-75,-5); echo "<br>"; echo "The division of 120 and -30: ".divTwoNumbers(120,-30); echo "<br>"; //exit php ?>
When the above code is executed, it produces the following result
The product of 15 and 3: 5 The product of -75 and -5: 15 The product of 120 and -30: -4
Calculate division of two numbers using function – #2
In this program, the user declare two integer variables $x and $y as parameters of the user defined function. Then two numbers are divided using division(/) operator. finally when the user call the function with passing argument, the result is displayed on the screen
Program 2
<?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 150 and 10: "; echo divTwoNumbers(150,10); echo "<br>"; echo "The division of 25 and -5: "; echo divTwoNumbers(25,-5); echo "<br>"; echo "The division of -180 and -12: "; echo divTwoNumbers(-180,-12); echo "<br>"; //exit php ?>
When the above code is executed, it produces the following result
The division of 150 and 10: 15 The division of 25 and -5: -5 The division of -180 and -12: 15
Similar post
Java program to divide two numbers
C program to divide two numbers
C++ program to divide two numbers
Python program to divide two numbers
Java program to divide two floating-point numbers
C program to divide two floating-point numbers