Arithmetic Caculation

PHP multiply two numbers using function

PHP multiply two numbers using function

In this article, we will discuss the concept of PHP multiply two numbers using function

In this post, we are going to learn how to write a program to find  multiplication of two numbers using function in PHP language

Code to find product of two numbers in PHP

Product of two numbers using function  – #1

Program 1

  • Start PHP script
  • Declare two integer variable as parameter
  • Calculate product 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

<html>
<head>
<title> product of two numbers</title>
</head>
<body>
<h3>Multiply two number using function</h3>
<?php //php script
//function definition with parameter
function mulTwoNumbers($x, $y)
{
  return $x * $y;
  //calculate the product of two numbers using function
  //and return the product of given number
}
//Calling the function with argument to print result
echo "The product of 5 and 12: ".mulTwoNumbers(5,12);
echo "<br>";
echo "The product of 5 and -10: ".mulTwoNumbers(5,-10);
echo "<br>";
echo "The product of -12 and -13: ".mulTwoNumbers(-12,-13);
echo "<br>";
//exit php
?>
</body>
</html>

When the above code is executed, it produces the following result

Multiply two number using function

The product of 5 and 12: 60
The product of 5 and -10: -50
The product of -12 and -13: 156

 

Product of two numbers using function  – #2

Program 2

  • Start PHP script
  • Declare two integer variable as parameter
  • Calculate product 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> product of two numbers</title>
</head>
<body>
<h3>Multiply two number using function</h3>
<?php //php script
//function definition with parameter
function mulTwoNumbers($x, $y)
{
  $mul=$x * $y;
  //calculate product of two numbers 
  //using multiplicaion(*) operator
  return $mul;//return the result to main
  echo $mul;//print the result when calling the function
}

//Calling the function and print result
echo "The product of 15 and 10: ";
echo mulTwoNumbers(15,10);
echo "<br>";
echo "The product of 25 and 10: ";
echo mulTwoNumbers(25,10);
echo "<br>";
echo "The product of 15 and -12: ";
echo mulTwoNumbers(15,-12);
echo "<br>";
//exit php
?>

When the above code is executed, it produces the following result

Multiply two number using function

The product of 15 and 10: 150
The product of 25 and 10: 250
The product of 15 and -12: -180

 

 

Similar post

Java program to multiply two numbers

C++ program to multiply two numbers

C program to multiply two numbers

Python program to multiply two numbers

 

Java program to multiply two floating-point numbers

C++ program to multiply two floating-point numbers

C program to multiply two floating-point numbers

Python program to multiply two floating-point numbers

 

PHP addition of two numbers

PHP addition of two numbers using function

PHP subtraction of two numbers

PHP subtraction of two numbers using function

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.