Addition of two numbers without propagating Carry | Function to print sum of 2 numbers without propagating carry ; Reverse a ; Reverse b ; Generate sum Since length of both a and b are same , take any one of them . ; Extract digits from a and b and add ; If sum is single digit ; If sum is not single digit reverse sum ; Extract digits from sum and append to result ; Driver code
< ? php function printSum ( $ a , $ b ) { $ res = 0 ; $ temp1 = 0 ; $ temp2 = 0 ; while ( $ a != 0 ) { $ temp1 = $ temp1 * 10 + ( $ a % 10 ) ; $ a = ( int ) ( $ a / 10 ) ; } $ a = $ temp1 ; while ( $ b != 0 ) { $ temp2 = $ temp2 * 10 + ( $ b % 10 ) ; $ b = ( int ) ( $ b / 10 ) ; } $ b = $ temp2 ; while ( $ a != 0 ) { $ sum = ( $ a % 10 + $ b % 10 ) ; if ( ( int ) ( $ sum / 10 ) == 0 ) { $ res = $ res * 10 + $ sum ; } else { $ temp1 = 0 ; while ( $ sum != 0 ) { $ temp1 = $ temp1 * 10 + ( $ sum % 10 ) ; $ sum = ( int ) ( $ sum / 10 ) ; } $ sum = $ temp1 ; while ( $ sum != 0 ) { $ res = $ res * 10 + ( $ sum % 10 ) ; $ sum = ( int ) ( $ sum / 10 ) ; } } $ a = ( int ) ( $ a / 10 ) ; $ b = ( int ) ( $ b / 10 ) ; } return $ res ; } $ a = 7752 ; $ b = 8834 ; echo ( printSum ( $ a , $ b ) ) ; ? >