Add two integers of different base and represent sum in smaller base of the two | Javascript Program to find the sum of two integers of different bases . ; Initialize power of base ; Initialize result ; Decimal equivalent is str [ len - 1 ] * 1 + str [ len - 2 ] * base + str [ len - 3 ] * ( base ^ 2 ) + ... ; Maximum base - 36 String base_num = " " ; ; print ans in reverse order ; Driver Code ; Integer in base 10 ; Sum of integers ; Minimum Base ; Sum of integers in Min Base
function val ( c ) { if ( c >= ' ' && c <= ' ' ) return c . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; else return c . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) + 10 ; } function convert ( num , bases ) { let len = ( num . length ) ; let power = 1 ; let res = 0 ; let i ; for ( i = len - 1 ; i >= 0 ; i -- ) { res += val ( num [ i ] ) * power ; power = power * bases ; } return res ; } function dec_to_base ( num , bases ) { let base_num = [ ] ; while ( num > 0 ) { let dig = ( num % bases ) ; if ( dig < 10 ) base_num . push ( ( dig ) . toString ( ) ) ; else base_num . append ( ( ' ' . charCodeAt ( 0 ) + dig - 10 ) . toString ( ) ) ; num = Math . floor ( num / bases ) ; } for ( let i = base_num . length - 1 ; i >= 0 ; i -- ) document . write ( base_num [ i ] ) ; } let a = " " ; let b = " " ; let base_a = 6 ; let base_b = 8 ; let a10 = convert ( a , base_a ) ; let b10 = convert ( b , base_b ) ; let summ = a10 + b10 ; let min_base = Math . min ( base_a , base_b ) ; dec_to_base ( summ , min_base ) ;