Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Function to divide two numbers:PHP program

Function to divide two numbers:PHP program

Posted on July 23, 2022

Table of Contents

  • Function to divide two numbers:PHP program
    • Code to find division of two numbers in PHP
      • Division of two numbers using function  – #1
      • Division of two numbers using function  – #2

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 

 

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes