# Check whether product of digits at even places is divisible by sum of digits at odd place of a number

Given a number N and numbers of digits in N, the task is to check whether the product of digits at even places of a number is divisible by sum of digits at odd place. If it is divisible, output “TRUE” otherwise output “FALSE”.

**Examples:**

Input:N = 2157Output:TRUE Since, 1 * 7 = 7, which is divisible by 2+5=7Input:N = 1234Output:TRUE Since, 2 * 4 = 8, which is divisible by 1 + 3 = 4

**Approach:**

- Find product of digits at even places from right to left.
- Find sum of digits at odd places from right to left.
- Then check the divisibility of product by taking it’s modulo with sum
- If modulo gives 0, output TRUE, otherwise output FALSE

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// below function checks whether ` `// product of digits at even places ` `// is divisible by sum of digits at odd places ` `bool` `productSumDivisible(` `int` `n, ` `int` `size) ` `{ ` ` ` `int` `sum = 0, product = 1; ` ` ` `while` `(n > 0) { ` ` ` ` ` `// if size is even ` ` ` `if` `(size % 2 == 0) { ` ` ` `product *= n % 10; ` ` ` `} ` ` ` ` ` `// if size is odd ` ` ` `else` `{ ` ` ` `sum += n % 10; ` ` ` `} ` ` ` `n = n / 10; ` ` ` `size--; ` ` ` `} ` ` ` ` ` `if` `(product % sum == 0) ` ` ` `return` `true` `; ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 1234; ` ` ` `int` `len = 4; ` ` ` ` ` `if` `(productSumDivisible(n, len)) ` ` ` `cout << ` `"TRUE"` `; ` ` ` `else` ` ` `cout << ` `"FALSE"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// JAVA implementation of the above approach ` ` ` `class` `GFG { ` ` ` ` ` `// below function checks whether ` ` ` `// product of digits at even places ` ` ` `// is divisible by sum of digits at odd places ` ` ` `static` `boolean` `productSumDivisible(` `int` `n, ` `int` `size) ` ` ` `{ ` ` ` `int` `sum = ` `0` `, product = ` `1` `; ` ` ` `while` `(n > ` `0` `) { ` ` ` ` ` `// if size is even ` ` ` `if` `(size % ` `2` `== ` `0` `) { ` ` ` `product *= n % ` `10` `; ` ` ` `} ` ` ` ` ` `// if size is odd ` ` ` `else` `{ ` ` ` `sum += n % ` `10` `; ` ` ` `} ` ` ` `n = n / ` `10` `; ` ` ` `size--; ` ` ` `} ` ` ` ` ` `if` `(product % sum == ` `0` `) { ` ` ` `return` `true` `; ` ` ` `} ` ` ` `return` `false` `; ` ` ` `} ` ` ` `// Driver code ` ` ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `1234` `; ` ` ` `int` `len = ` `4` `; ` ` ` ` ` `if` `(productSumDivisible(n, len)) { ` ` ` `System.out.println(` `"TRUE"` `); ` ` ` `} ` ` ` `else` `{ ` ` ` `System.out.println(` `"FALSE"` `); ` ` ` `} ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python 3

# Python 3 implementation of the above approach

# Below function checks whether product

# of digits at even places is divisible

# by sum of digits at odd places

def productSumDivisible(n, size):

sum = 0

product = 1

while (n > 0) :

# if size is even

if (size % 2 == 0) :

product *= n % 10

# if size is odd

else :

sum += n % 10

n = n // 10

size -= 1

if (product % sum == 0):

return True

return False

# Driver code

if __name__ == “__main__”:

n = 1234

len = 4

if (productSumDivisible(n, len)):

print(“TRUE”)

else :

print(“FALSE”)

# This code is contributed by ChitraNayal

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// below function checks whether ` ` ` `// product of digits at even places ` ` ` `// is divisible by K ` ` ` `static` `bool` `productSumDivisible(` `int` `n, ` `int` `size) ` ` ` `{ ` ` ` `int` `sum = 0, product = 1; ` ` ` `while` `(n > 0) { ` ` ` ` ` `// if size is even ` ` ` `if` `(size % 2 == 0) { ` ` ` `product *= n % 10; ` ` ` `} ` ` ` ` ` `// if size is odd ` ` ` `else` `{ ` ` ` `sum += n % 10; ` ` ` `} ` ` ` `n = n / 10; ` ` ` `size--; ` ` ` `} ` ` ` ` ` `if` `(product % sum == 0) { ` ` ` `return` `true` `; ` ` ` `} ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 1234; ` ` ` `int` `len = 4; ` ` ` ` ` `if` `(productSumDivisible(n, len)) ` ` ` `Console.WriteLine(` `"TRUE"` `); ` ` ` `else` ` ` `Console.WriteLine(` `"FALSE"` `); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the above approach ` ` ` `// Below function checks whether ` `// product of digits at even ` `// places is divisible by sum of ` `// digits at odd places ` `function` `productSumDivisible(` `$n` `, ` `$size` `) ` `{ ` ` ` `$sum` `= 0; ` `$product` `= 1; ` ` ` `while` `(` `$n` `> 0) ` ` ` `{ ` ` ` ` ` `// if size is even ` ` ` `if` `(` `$size` `% 2 == 0) ` ` ` `{ ` ` ` `$product` `*= ` `$n` `% 10; ` ` ` `} ` ` ` ` ` `// if size is odd ` ` ` `else` ` ` `{ ` ` ` `$sum` `+= ` `$n` `% 10; ` ` ` `} ` ` ` `$n` `= ` `$n` `/ 10; ` ` ` `$size` `--; ` ` ` `} ` ` ` ` ` `if` `(` `$product` `% ` `$sum` `== 0) ` ` ` `return` `true; ` ` ` `return` `false; ` `} ` ` ` `// Driver code ` `$n` `= 1234; ` `$len` `= 4; ` ` ` `if` `(productSumDivisible(` `$n` `, ` `$len` `)) ` ` ` `echo` `"TRUE"` `; ` `else` ` ` `echo` `"FALSE"` `; ` ` ` `// This code is contributed by anuj_67.. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

TRUE

## Recommended Posts:

- Check whether product of digits at even places of a number is divisible by K
- Check whether sum of digits at odd places of a number is divisible by K
- Check if product of digits of a number at even and odd places is equal
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Program to check if a number is divisible by sum of its digits
- Check if N is divisible by a number which is composed of the digits from the set {A, B}
- Program to check if a number is divisible by any of its digits
- Given a large number, check if a subsequence of digits is divisible by 8
- Find the sum of digits of a number at even and odd places
- Primality test for the sum of digits at odd places of a number
- Largest number with the given set of N digits that is divisible by 2, 3 and 5
- Find N digits number which is divisible by D
- Smallest number with sum of digits as N and divisible by 10^N
- Number of digits in the product of two numbers
- Total number of ways to place X and Y at n places such that no two X are together

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.