Find the sum of digits of a number at even and odd places | Java implementation of the approach ; Function to find the sum of the odd and even positioned digits in a number ; To store the respective sum ; Converting integer to String ; Traversing the String ; Driver code
import java . util . * ; class GFG { static void getSum ( int n ) { int sumOdd = 0 ; int sumEven = 0 ; String num = String . valueOf ( n ) ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) if ( i % 2 == 0 ) sumOdd = sumOdd + ( num . charAt ( i ) - '0' ) ; else sumEven = sumEven + ( num . charAt ( i ) - '0' ) ; System . out . println ( " Sum ▁ odd ▁ = ▁ " + sumOdd ) ; System . out . println ( " Sum ▁ even ▁ = ▁ " + sumEven ) ; } public static void main ( String [ ] args ) { int n = 457892 ; getSum ( n ) ; } }