Skip to content
Related Articles
Check whether sum of digits at odd places of a number is divisible by K
• Last Updated : 31 May, 2021

Given two integers ‘N’ and ‘K’, the task is to find the sum of digits of ‘N’ at its odd places (right to left) and check whether the sum is divisible by ‘K’. If it is divisible, output YES, otherwise output NO.

Examples:

Input: N = 4325, K = 4
Output: YES
Since, 3 + 5 = 8, which is divisible by 4.

Input: N = 1209, K = 3
Output: NO

Approach:

• Find the sum of the digits of ‘N’ at odd places (right to left).
• Then check the divisibility of the sum by taking its modulo with ‘K’.
• If it is divisible, then output ‘YES’, otherwise output ‘NO’.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// function that checks the``// divisibility of the sum``// of the digits at odd places``// of the given number``bool` `SumDivisible(``int` `n, ``int` `k)``{``    ``int` `sum = 0, position = 1;``    ``while` `(n > 0) {` `        ``// if position is odd``        ``if` `(position % 2 == 1)``            ``sum += n % 10;``        ``n = n / 10;``        ``position++;``    ``}` `    ``if` `(sum % k == 0)``        ``return` `true``;``    ``return` `false``;``}` `// Driver code``int` `main()``{``    ``int` `n = 592452;``    ``int` `k = 3;` `    ``if` `(SumDivisible(n, k))``        ``cout << ``"YES"``;``    ``else``        ``cout << ``"NO"``;``    ``return` `0;``}`

## Java

 `// Java implementation of the approach``import` `java.util.*;` `class` `solution``{` `// function that checks the``// divisibility of the sum``// of the digits at odd places``// of the given number``static` `boolean` `SumDivisible(``int` `n, ``int` `k)``{``    ``int` `sum = ``0``, position = ``1``;``    ``while` `(n > ``0``) {` `        ``// if position is odd``        ``if` `(position % ``2` `== ``1``)``            ``sum += n % ``10``;``        ``n = n / ``10``;``        ``position++;``    ``}` `    ``if` `(sum % k == ``0``)``        ``return` `true``;``    ``return` `false``;``}` `// Driver code``public` `static` `void` `main(String arr[])``{``    ``int` `n = ``592452``;``    ``int` `k = ``3``;` `    ``if` `(SumDivisible(n, k))``        ``System.out.println(``"YES"``);``    ``else``        ``System.out.println(``"NO"``);` `}``}``//This code is contributed by Surendra_Gangwar`

## Python 3

 `# Python 3 implementation of the approach` `# function that checks the divisibility``# of the sum of the digits at odd places``# of the given number``def` `SumDivisible(n, k):` `    ``sum` `=` `0``    ``position ``=` `1``    ``while` `(n > ``0``) :` `        ``# if position is odd``        ``if` `(position ``%` `2` `=``=` `1``):``            ``sum` `+``=` `n ``%` `10``        ``n ``=` `n ``/``/` `10``        ``position ``+``=` `1``    ` `    ``if` `(``sum` `%` `k ``=``=` `0``):``        ``return` `True``    ``return` `False` `# Driver code``if` `__name__ ``=``=``"__main__"``:``    ``n ``=` `592452``    ``k ``=` `3` `    ``if` `(SumDivisible(n, k)):``        ``print``(``"YES"``)``    ``else``:``        ``print``(``"NO"``)` `# This code is contributed``# by ChitraNayal`

## C#

 `// C# implementation of the approach``using` `System;` `class` `GFG``{``// function that checks the``// divisibility of the sum``// of the digits at odd places``// of the given number``static` `bool` `SumDivisible(``int` `n, ``int` `k)``{``    ``int` `sum = 0, position = 1;``    ``while` `(n > 0)``    ``{` `        ``// if position is odd``        ``if` `(position % 2 == 1)``            ``sum += n % 10;``        ``n = n / 10;``        ``position++;``    ``}` `    ``if` `(sum % k == 0)``        ``return` `true``;``    ``return` `false``;``}` `// Driver code``static` `public` `void` `Main ()``{``    ``int` `n = 592452;``    ``int` `k = 3;` `    ``if` `(SumDivisible(n, k))``        ``Console.WriteLine(``"YES"``);``    ``else``        ``Console.WriteLine(``"NO"``);``}``}` `// This code is contributed by Sachin`

## PHP

 ` 0)``    ``{` `        ``// if position is odd``        ``if` `(``\$position` `% 2 == 1)``            ``\$sum` `+= ``\$n` `% 10;``        ``\$n` `= (int)``\$n` `/ 10;``        ``\$position``++;``    ``}` `    ``if` `(``\$sum` `% ``\$k` `== 0)``        ``return` `true;``    ``return` `false;``}` `// Driver code``\$n` `= 592452;``\$k` `= 3;` `if` `(SumDivisible(``\$n``, ``\$k``))``    ``echo` `"YES"``;``else``    ``echo` `"NO"``;` `// This code is contributed``// by Sach_Code``?>`

## Javascript

 ``
Output:
`YES`

#### Method #2:Using string() method:

1. Convert the integer to a string, then traverse the string and find the sum of all odd indices by storing it in sum.
2. If the sum is divisible by k, then return True else False.

Below is the implementation:

## Python3

 `# Python3 implementation of the``# above approach` `def` `sumDivisible(n, k):``    ``sum` `=` `0``    ` `    ``# Converting integer to string``    ``num ``=` `str``(n)``    ` `    ``# Traversing the string``    ``for` `i ``in` `range``(``len``(num)):``        ``if``(i ``%` `2` `!``=` `0``):``            ``sum` `=` `sum``+``int``(num[i])` `    ``if` `sum` `%` `k ``=``=` `0``:``        ``return` `True``    ``return` `False`  `# Driver code``n ``=` `592452``k ``=` `3``if` `sumDivisible(n, k) ``=``=` `True``:``    ``print``(``"YES"``)``else``:``    ``print``(``"NO"``)` `# This code is contributed by vikkycirus`

## Javascript

 ``

Output:

`Yes`

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up