Skip to content

Sum of Odd and Even Digits of a Number

Latest

Choose a tag to compare

@divyaskesavan divyaskesavan released this 22 Feb 16:08
· 1 commit to main since this release
cdb8a67

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
int number = sc.nextInt();
int evenSum = 0;
int oddSum = 0;
while (number > 0) {
int digit = number % 10;
if (digit % 2 == 0) {
evenSum += digit;
} else {
oddSum += digit;
}
number /= 10;
}
System.out.println("Sum of even digits: " + evenSum);
System.out.println("Sum of odd digits: " + oddSum);
}
}