Last Updated: 4 Feb, 2021

Sum of even & odd

Easy

Problem statement

Write a program to input an integer N and print the sum of all its even digits and all its odd digits separately.

Digits mean numbers, not the places! If the given integer is "13245", the even digits are 2 and 4, and the odd digits are 1, 3, and 5.

Input format:
 The only line of input contains a single integer N.
Output format:
Print first even sum and then odd sum separated by space.
Constraints
0 <= N <= 10^8

Approaches

01 Approach

First, initialize two variables, one as evenSum=0, and the other oddSum=0. Then, For the given input n iterate till n > 0. To get the last digit do, last = n % 10. If your last is even add it to evenSum else add it to oddSum. Now update n as n = n/10.


Finally, print out the values of the variable evenSum and oddSum.