Sum of Even & Odd Digits Problem Statement

Create a program that accepts an integer N and outputs two sums: the sum of all even digits and the sum of all odd digits found in the integer.

Explanation:

Digits refer to the numbers themselves, not their positional values. For example, if N is "13245", the even digits are 2 & 4, and the odd digits are 1, 3 & 5.

Input:

Integer N

Output:

Sum_of_Even_Digits Sum_of_Odd_Digits

First print the sum of even digits, followed by the sum of odd digits, separated by a space.

Example:

Input:
13245
Output:
6 9
Explanation:

In the input 13245, the even digits are 2 and 4, summing to 6. The odd digits are 1, 3, and 5, summing to 9.

Constraints:

  • 0 <= N <= 10^8
Patric Andrade
6mo
k=[1,3,2,4,5] even = [] odd = [] for i in k: #print(i) if i % 2 == 0: even.append(i) else: odd.append(i) print("odd: ", odd) print("even: ", even)
Shubham Raj Kumawat
1y

import java.util.Scanner;

public class SumEvenAndOddDigits {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter an integer N: ");

int N = scanner.nextInt();

int evenSum = 0;

int oddSum = 0;

while (N != 0) {

int digit = N % 10;

if (digit % 2 == 0) {

evenSum += digit;

} else {

oddSum += digit;

}

N /= 10;

}

System.out.println("Sum of even digits: " + evenSum);

System.out.println("Sum of odd digits: " + oddSum);

}

}

Abhijit Das
1y
a = int(input()) n = [] for _ in range(a): m = int(input()) n.append(m) even = 0 odd = 0 for x in range(0, len(n)): if n[x] % 2 == 0: even = even + n[x] else: odd = odd + n[x] print(even) print(odd) M...read more
Komari Bhudevi
1y

Namepace SumOfEvenAndOddDigits

{

class Program

{

static void Main(string[] args)

{

Console.Write("Enter an integer: ");

int number = int.Parse(Console.ReadLine());

int sumEven = 0;

int sumOdd = 0;

// C...read more

4JN19IS096 -Spandana K R
1y

def sum_of_even_and_odd_digits(N):

even_sum = 0

odd_sum = 0

while N > 0:

digit = N % 10 # Extract the last digit

if digit % 2 == 0: # Check if the digit is even

even_sum += digit

else:

odd_sum += digi...read more

Add answer anonymously...
Accenture Application Development Associate Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter