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 means numbers not the places. That is,
if the given integer is "13245", even digits are
2 & 4 and odd digits are 1, 3 & 5.
Sample Input :
1234
Sample Output :
6 4
using namespace std;
int main()
{
int n, se = 0, sd = 0, r;
cin>>n;
while ( n> 0)
{
r = n % 10;
if (r % 2 == 0)
{
se = se + r;
}
else
{
sd = sd + r;
}
n = n/ 10;
}
cout << se << " " <<sd;
return 0;
}
Digits means numbers not the places. That is,
if the given integer is "13245", even digits are
2 & 4 and odd digits are 1, 3 & 5.
Sample Input :
1234
Sample Output :
6 4
C++ code :-
#include<iostream>using namespace std;
int main()
{
int n, se = 0, sd = 0, r;
cin>>n;
while ( n> 0)
{
r = n % 10;
if (r % 2 == 0)
{
se = se + r;
}
else
{
sd = sd + r;
}
n = n/ 10;
}
cout << se << " " <<sd;
return 0;
}
sum of even digits |
some related posts :-
2. print triangle number pattern.
1
11
111
1111
3. check whether the character is an uppercase or lowercase.
4. find the average marks of three subject in c++
1
11
111
1111
3. check whether the character is an uppercase or lowercase.
4. find the average marks of three subject in c++
can i get a python code for this question?
ReplyDeleteWrite a program to input an integer N and print the sum of all its even digits and sum of all its odd digits separately.