Last Updated: 25 Nov, 2020

Sum of even & odd

Easy
Asked in company
Delta Air Lines

Problem statement

You are given an integer 'n'. Calculate the sum of even and odd digits of 'n', represented in decimals.


Even and odd do not refer to the position of the digit but to the polarity of the digit.


Return the answer in the form of an array of size 2, such that the 0th index represents the even sum and the 1st index represents the odd sum.


Example:
Input: 'n' = 1986.

Output: [14, 10]

Explanation: Even digits are 8 & 6, and odd digits are 1 & 9. The sum of even digits = 14, and the sum of odd digits = 10.
Input format :
The first and only line of input contains the integer 'n'.
Output format :
The output consists of a single line containing two space-separated integers denoting the sum of even digits and the sum of odd digits, respectively.

Note:

You do not need to print anything, it has already been taken care of. Just implement the given function.

Approaches

01 Approach

Use the modulo operation to find the digits of numbers.

 

Algorithm:

 

  • First, make an iteration till the given integer ‘num’ is greater than 0.
  • Store the one’s place digit in a variable.
  • Check whether the digit is even or odd
    • If the digit is even, add the digit in the ‘evenSum’.
    • If the digit is odd, add the digit in the ‘oddSum’.
  • Now, remove the one’s place digit from ‘num’.