PHP Program to Condense a Number

Last Updated:

Condense a Number

In the following example, we will condense a number (8654) into a single digit (5).

i.e., 8654 = 8 + 6 + 5 + 4
8654 = 23
8654 = 2 + 3
8654 = 5 (single digit)

Example

PHP Compiler
<?php $num = 8654; $userValue = $num; function myCondense($a){ $copyNum = $a; $remainder = 0; $result = 0; while($copyNum != 0) { $remainder = $copyNum % 10; $result += $remainder; $copyNum = floor($copyNum / 10); } return $result; } while($num > 9) $num = myCondense($num); echo "Single Digit of $userValue: $num"; ?>

Share this Page

Meet the Author