Check if a number is magic ( Recursive sum of digits is 1 ) | PHP program to check if a number is Magic number . ; Note that the loop continues if n is 0 and sum is non - zero . It stops when n becomes 0 and sum becomes single digit . ; Return true if sum becomes 1. ; Driver code
< ? php function isMagic ( $ n ) { $ sum = 0 ; while ( $ n > 0 $ sum > 9 ) { if ( $ n == 0 ) { $ n = $ sum ; $ sum = 0 ; } $ sum += $ n % 10 ; $ n /= 10 ; } return ( $ sum == 1 ) ; } $ n = 1234 ; if ( isMagic ( $ n ) ) echo " Magic ▁ Number " ; else echo " Not ▁ a ▁ magic ▁ Number " ; ? >