Check whether sum of digits at odd places of a number is divisible by K | C ++ implementation of the above approach ; Converting integer to string ; Traversing the string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool sumDivisible ( int n , int k ) { int sum = 0 ; string num = to_string ( n ) ; int i ; for ( i = 0 ; i < num . size ( ) ; i ++ ) { if ( i % 2 != 0 ) { sum = sum + ( num [ i ] - '0' ) ; } } if ( sum % k == 0 ) { return true ; } else { return false ; } } int main ( ) { int n = 592452 ; int k = 3 ; if ( sumDivisible ( n , k ) ) { cout << ( " YES " ) ; } else { cout << ( " NO " ) ; } return 0 ; }