
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP program to calculate the repeated subtraction of two numbers
To calculate the repeated subtraction of two numbers, the code is as follows −
Example
<?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.
- Related Articles
- 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
- 8085 Program for subtraction of multi-Byte BCD numbers
- 8086 program to determine subtraction of corresponding elements of two arrays
- Calculate Subtraction of diagonals-summations in a two-dimensional matrix using JavaScript
- Find the square roots of 121 and 169 by the method of repeated subtraction.
- What is the subtraction of binary numbers?
- Java Program to Calculate the Sum of Natural Numbers
- Swift Program to Calculate the Sum of Natural Numbers
- Kotlin Program to Calculate the Sum of Natural Numbers
- Haskell program to calculate the sum of natural numbers
- Program for subtraction of multi-byte BCD numbers in 8085 Microprocessor
- Java program to calculate mean of given numbers
- C++ Program to Calculate Sum of Natural Numbers

Advertisements