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
function printSum ( a , b ) { var res = 0 ; var temp1 = 0 , temp2 = 0 ; while ( a ) { temp1 = temp1 * 10 + ( a % 10 ) ; a = parseInt ( a / 10 ) ; } a = temp1 ; while ( b ) { temp2 = temp2 * 10 + ( b % 10 ) ; b = parseInt ( b / 10 ) ; } b = temp2 ; while ( a ) { var sum = ( a % 10 + b % 10 ) ; if ( parseInt ( sum / 10 ) == 0 ) res = res * 10 + sum ; else { temp1 = 0 ; while ( sum ) { temp1 = temp1 * 10 + ( sum % 10 ) ; sum = parseInt ( sum / 10 ) ; } sum = temp1 ; while ( sum ) { res = res * 10 + ( sum % 10 ) ; sum = parseInt ( sum / 10 ) ; } } a = parseInt ( a / 10 ) ; b = parseInt ( b / 10 ) ; } return res ; } var a = 7752 , b = 8834 ; document . write ( printSum ( a , b ) ) ;