-
Notifications
You must be signed in to change notification settings - Fork 0
/
sumOfOddAndEven.java
47 lines (43 loc) · 1.33 KB
/
sumOfOddAndEven.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Write a program to input an integer N and print the sum of all its even digits and sum of all its odd digits separately.
Digits mean numbers, not the places! That is, if the given integer is "13245", even digits are 2 & 4 and odd digits are 1, 3 & 5.
Input format :
Integer N
Output format :
Sum_of_Even_Digits Sum_of_Odd_Digits
(Print first even sum and then odd sum separated by space)
Constraints
0 <= N <= 10^8
Sample Input 1:
1234
Sample Output 1:
6 4
Sample Input 2:
552245
Sample Output 2:
8 15
Explanation for Input 2:
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)
*/
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Write your code here
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int sum_e = 0 ;
int sum_O = 0;
while(n!=0){
int modulo = n%10;
if(modulo%2==0){
sum_e+=modulo;
}
if(modulo%2!=0){
sum_O+=modulo;
}
n = (int) (n/10);
}
System.out.print(sum_e + " " + sum_O);
}
}