Sum of an array of large numbers | Function to print the result of the summation of numbers having K - digit ; Reverse the array to obtain the result ; Print every digit of the answer ; Function to calculate the total sum ; Stores the array of large numbers in integer format ; Convert each element from character to integer ; Stores the carry ; Stores the result of summation ; Initialize the sum ; Calculate sum ; Update the sum by adding existing carry ; Store the number of digits ; Increase count of digits ; If the number exceeds 9 , Store the unit digit in carry ; Store the rest of the sum ; Append digit by digit into result array ; Append result until carry is 0 ; Print the result ; Driver Code ; Given N array of large numbers
function printResult ( result ) { result . reverse ( ) ; let i = 0 ; while ( i < result . length ) { document . write ( result [ i ] ) ; i ++ ; } } function sumOfLargeNumbers ( v , k , N ) { let x = [ ] ; for ( let i = 0 ; i < k ; i ++ ) x . push ( [ ] ) ; for ( let i = 0 ; i < k ; i ++ ) { for ( let j = 0 ; j < N ; j ++ ) { x [ i ] . push ( v [ i ] [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; } } let carry = 0 ; let result = [ ] ; for ( let i = N - 1 ; i >= 0 ; i -- ) { let sum = 0 ; for ( let j = 0 ; j < k ; j ++ ) sum += x [ j ] [ i ] ; sum += carry ; let temp = sum ; let count = 0 ; while ( temp > 9 ) { temp = temp % 10 ; count ++ ; } let l = Math . pow ( 10 , count ) ; if ( l != 1 ) carry = Math . floor ( sum / l ) ; sum = sum % 10 ; result . push ( sum ) ; } while ( carry != 0 ) { let a = carry % 10 ; result . push ( a ) ; carry = Math . floor ( carry / 10 ) ; } printResult ( result ) ; } let K = 10 ; let N = 5 ; let arr = [ " " , " " , " " , " " , " " ] ; sumOfLargeNumbers ( arr , N , K ) ;