Calculations

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

PHP program to divide 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

C++ program to divide two floating-point numbers

Python program to divide two floating-point numbers

PHP dividing two numbers: PHP program
JavaScript Program to substract Two Numbers
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago