Arithmetic Caculation

Function to divide two numbers:PHP program

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 

 

 

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.