sum of even or odd digits in a given numbers in c++

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


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 and odd digits in c++,sum of even digits in c++
sum of even digits


















some related posts :-









SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

1 comments:

  1. can i get a python code for this question?
    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.

    ReplyDelete