Sum of even & odd

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 mean numbers, not the places! That is, if the given integer is "13245", even digits are 2 & 4 and odd digits are 1, 3 & 5.
Input format :
 Integer N
Output format :
Sum_of_Even_Digits Sum_of_Odd_Digits
(Print first even sum and then odd sum separated by space)
Constraints
0 <= N <= 10^8
CodingNinjas
author
2y
Space Complexity: O(1)Explanation: Time Complexity: O(1)Explanation:
Patric Andrade
5mo
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
Get AmbitionBox app

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