- Related Questions & Answers
- Java program to calculate the product of two numbers
- PHP program to calculate the sum of square of first n natural numbers
- Write a program to calculate the least common multiple of two numbers JavaScript
- 8086 program to determine subtraction of corresponding elements of two arrays
- 8085 Program for subtraction of multi-Byte BCD numbers
- Java program to calculate the average of numbers in Java
- Java program to calculate mean of given numbers
- C++ Program to Calculate Sum of Natural Numbers
- C++ Program to Calculate Average of Numbers Using Arrays
- Program for subtraction of multi-byte BCD numbers in 8085 Microprocessor
- Java program to find the LCM of two numbers
- Adding two arrays of objects with existing and repeated members of two JavaScript arrays replacing the repeated ones
- C Program for subtraction of matrices
- PHP program to calculate the total time given an array of times
- Java program to print the Armstrong numbers between two numbers

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

<?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); ?>

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.

Advertisements