-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ30.cpp
More file actions
34 lines (24 loc) · 732 Bytes
/
Q30.cpp
File metadata and controls
34 lines (24 loc) · 732 Bytes
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
// 30. Write a program to input an integer 'n' and print the sum of all its even digits and the sum of all its odd digits separately. Example : Input: n = 132456, Output: 12, 9
#include <iostream>
using namespace std;
void sumEvenOddDigits(int n) {
int evenSum = 0, oddSum = 0;
while (n > 0) {
int digit = n % 10;
if (digit % 2 == 0) {
evenSum += digit;
} else {
oddSum += digit;
}
n /= 10;
}
cout << "Sum of even digits: " << evenSum << endl;
cout << "Sum of odd digits: " << oddSum << endl;
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
sumEvenOddDigits(n);
return 0;
}