PHP program to calculate the repeated subtraction of two numbers


To calculate the repeated subtraction of two numbers, the code is as follows −

Example

 Live Demo

<?php
function repeated_sub($val_1, $val_2)
{
   if ($val_1 % $val_2 == 0)
      return floor(((int)$val_1 / $val_2));
   return floor(((int)$val_1 / $val_2) + repeated_sub($val_2, $val_1 % $val_2));
}
$val_1 = 1000;
$val_2 = 189;
print_r("The repeated subtraction results in ");
echo repeated_sub($val_1, $val_2);
?>

Output

The repeated subtraction results in 18

A function named ‘repeated_sub’ is defined that checks to see if two values divide each other completely, and if this is true, it divides the numbers and gives the floor value of the quotient. Otherwise, it gives the floor value of quotient and the value computed by calling the ‘repeated_sub’ function on the second value, and the remainder when the values are divided.

Outside the function, values are given to both the variables and the function is called by passing these values to the function as parameter. The output is displayed on the console.

Updated on: 02-Jul-2020

136 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements