The sum of even & odd
The sum of even & odd
Send Feedback
The sum of even & odd
Integer N
Sum_of_Even_Digits Sum_of_Odd_Digits
(Print first even sum and then odd sum separated by space)
0 <= N <= 10^8
1234
6 4
552245
8 15
For the given input, the even digits are 2, 2 and 4 and if we take the sum of these digits it will come out to be 8(2 + 2 + 4) and similarly, if we look at the odd digits, they are, 5, 5 and 5 which makes a sum of 15(5 + 5 + 5). Hence the answer would be, 8(evenSum) <single space> 15(oddSum)
#include<iostream>using namespace std;
int main() { // Write your code here int n,sum_e=0,sum_o=0, rem; cin>>n; while(n>0){ rem=n%10; n=n/10; if(rem%2==0) sum_e+=rem; else sum_o+=rem; } cout<<sum_e<<" "<<sum_o; }
Comments
Post a Comment