Computer Science, asked by abhaysagar011, 9 months ago

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 kumar24jun
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 SaurabhJacob
3

This program is in python language,

  1. n=input("Enter the number = ")
  2. le=len(n)
  3. n=int(n)
  4. sum_even=0
  5. sum_odd=0
  6. for i in range(1,le+1):
  7.    if((n%10)%2==0):
  8.            sum_even+=n%10
  9.    else:
  10.            sum_odd+=n%10
  11.    n=n//(10)
  12. 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