Write a PHP program to add the digits of a positive integer repeatedly until the result has a single digit.
Input : 48
For example given number is 59, the result will be 5.Step 1: 5 + 9 = 14Step 2: 1 + 4 = 5
<?php function add_digits($num) { if ( $num > 0) { return ($num - 1) % 9 + 1; } else { return 0; } } print_r(add_digits(48)."\n"); print_r(add_digits(59)."\n"); ?>
Sample Output:
3 5
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer