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.
Answers
Answered by
9
Explanation:
#include<iostream>
using namespace std;
int main()
{
int n, sumEven = 0, sumOdd = 0, r;
cin>>n;
while ( n> 0)
{
r = n % 10;
if (r % 2 == 0)
{
sumEven = sumEven + r;
}
else
{
sumOdd = sumOdd + r;
}
n = n/ 10;
}
cout << sumEven << " " <<sumOdd;
return 0;
}
Answered by
3
This program is in python language,
- n=input("Enter the number = ")
- le=len(n)
- n=int(n)
- sum_even=0
- sum_odd=0
- for i in range(1,le+1):
- if((n%10)%2==0):
- sum_even+=n%10
- else:
- sum_odd+=n%10
- n=n//(10)
- print("Sum of Even =",sum_even,"\nSum of ODD =",sum_odd )
Some important points,
- In the second line, storing the length of the integer entered by the user.
- In the sixth line, looping till the end of the number.
- In the seventh line, check for the even number at the end of the given number.
- In the eleventh line, remove the last digit of the number by floor division.
Similar questions