Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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 = 2157 
Output: TRUE
Since, 1 * 7 = 7, which is divisible by 2+5=7
Input: N = 1234
Output: TRUE
Since, 2 * 4 = 8, which is divisible by 1 + 3 = 4

Approach:  

  1. Find product of digits at even places from right to left.
  2. Find sum of digits at odd places from right to left.
  3. Then check the divisibility of product by taking it’s modulo with sum
  4. 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;
}


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");
        }
    }
}


Python3




# 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");
    }
}


Javascript




<script>
 
// Javascript 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)
{
    var 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 = parseInt(n / 10);
        size--;
    }
 
    if (product % sum == 0)
        return true;
    return false;
}
 
// Driver code
var n = 1234;
var len = 4;
if (productSumDivisible(n, len))
    document.write("TRUE");
else
    document.write("FALSE");
 
// This code is contributed by noob2000.
</script>


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..
?>


Output

TRUE






Time Complexity: O(logn)
Auxiliary Space: (1), since no extra space has been taken.

Method #2:Using string() method

Convert the integer to string then traverse the string and perform two operations

  • Multiply all odd indices and store them in product
  • Add all even indices and store them in sum
  • If the product is divisible by sum then return True else False

Below is the implementation:

C++




// C++ implementation of the above approach
#include <iostream>
 
using namespace std;
#include<string>
 
  // Below function checks whether product
  // of digits at even places is divisible
  // by sum of digits at odd places
bool  productSumDivisible(int n)
  {
    int sum = 0;
    int product = 1;
 
    // Converting integer to string
    string num = to_string(n);
 
    // Traversing the string
    for(int i = 0 ; i < num.length() ; i++ ) {
      if(i % 2 != 0){
        product = product*(num[i]);
      }
      else{
        sum = sum+int(num[i]);
      }
    }
 
    if (product % sum == 0){
      return true ;
    }
    else{
      return false;
    }       
  }
 
  // Driver code
 int main()
 {
 
    int n = 1234;
 
    if (productSumDivisible(n))
    {
      cout<<"true";
    }
    else{
     cout<<"false";
    }
   
}
 
// This code is contributed by akshitsaxena07


Java




// Java implementation of the above approach
import java.io.*;
 
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 sum = 0;
    int product = 1;
 
    // Converting integer to string
    String num = String.valueOf(n);
 
    // Traversing the string
    for(int i = 0 ; i < num.length() ; i++ ) {
      if(i % 2 != 0){
        product = product*Character.getNumericValue(num.charAt(i));
      }
      else{
        sum = sum+Character.getNumericValue(num.charAt(i));
      }
    }
 
    if (product % sum == 0){
      return true ;
    }
    else{
      return false;
    }       
  }
 
  // Driver code
  public static void main (String[] args) {
 
    int n = 1234;
 
    if (productSumDivisible(n))
    {
      System.out.println("TRUE");
    }
    else{
      System.out.println("FALSE");
    }
  }
}
 
// This code is contributed by rag2127.


Python3




# 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):
    sum = 0
    product = 1
     
    # Converting integer to string
    num = str(n)
     
    # Traversing the string
    for i in range(len(num)):
        if(i % 2 != 0):
            product = product*int(num[i])
        else:
            sum = sum+int(num[i])
 
    if (product % sum == 0):
        return True
    return False
 
 
# Driver code
if __name__ == "__main__":
    n = 1234
 
    if (productSumDivisible(n)):
        print("TRUE")
    else:
        print("FALSE")
 
# This code is contributed by vikkycirus


C#




// C# implementation of the above approach
using System;
 
class GFG{
     
// Below function checks whether product
// of digits at even places is divisible
// by sum of digits at odd places
static bool productSumDivisible(int n)
{
    int sum = 0;
    int product = 1;
     
    // Converting integer to string
    string num = n.ToString();
     
    // Traversing the string
    for(int i = 0; i < num.Length; i++ )
    {
        if (i % 2 != 0)
        {
            product = product*(int)Char.GetNumericValue(num[i]);
        }
        else
        {
            sum = sum+(int)Char.GetNumericValue(num[i]);
        }
    }
     
    if (product % sum == 0)
    {
        return true;
    }
    else
    {
        return false;
    }      
}
 
// Driver code
static public void Main()
{
    int n = 1234;
     
    if (productSumDivisible(n))
    {
        Console.WriteLine("TRUE");
    }
    else
    {
        Console.WriteLine("FALSE");
    }
}
}
 
// This code is contributed by avanitrachhadiya2155


Javascript




<script>
 
// JavaScript 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){
    var sum = 0
    var product = 1
      
    // Converting integer to string
    var num = n.toString();
      
    // Traversing the string
    for(i = 0 ; i < num.length ; i++ ) {
        if(i % 2 != 0){
            product = product*Number(num[i])
        }
        else{
            sum = sum+Number(num[i])
        }
    }
  
    if (product % sum == 0){
        return true ;
     }
    else{
    return false;
    }
}
  
  
// Driver code
   var  n = 1234
  
    if (productSumDivisible(n)){
        document.write("TRUE")
    }
    else{
        document.write("FALSE")
    }
     
</script>


Output

true






Time Complexity: O(d), where d is the number of digits in n
Auxiliary Space: (1), since no extra space has been taken.

 Using List Comprehension:

Approach:

This approach uses list comprehension to extract the even and odd digits from the input number. It then computes the product of the even digits and the sum of the odd digits and checks if the product of even digits is divisible by the sum of odd digits.

Convert the input integer N into a list of its digits by converting it to a string, iterating over the string, and converting each character back to an integer using the int() function. This is done using the expression digits = [int(digit) for digit in str(N)].
Extract the even-indexed digits (i.e., the digits at positions 0, 2, 4, etc.) into a separate list even_digits using slicing. This is done using the expression even_digits = digits[::2].
Extract the odd-indexed digits (i.e., the digits at positions 1, 3, 5, etc.) into a separate list odd_digits using slicing. This is done using the expression odd_digits = digits[1::2].
Initialize a variable even_product to 1, which will hold the product of the even digits.
Compute the sum of the odd digits using the sum() function and store it in the variable odd_sum.
Iterate over the even digits using a for loop and multiply them together to compute their product, which is stored in the variable even_product. This is done using the code for digit in even_digits: even_product *= digit.
Test whether the even product is divisible by the odd sum using the modulo operator %. If the remainder is 0, then the even product is divisible by the odd sum and the function returns True. Otherwise, the even product is not divisible by the odd sum and the function returns False. This is done using the expression return even_product % odd_sum == 0.

C++




#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
 
bool IsDivisible(int N)
{
    // Convert N to a list of digits
    vector<int> digits;
    int tempN = N;
    while (tempN > 0) {
        digits.push_back(tempN % 10);
        tempN /= 10;
    }
    reverse(digits.begin(), digits.end());
 
    // Separate even and odd positioned digits
    vector<int> evenDigits;
    vector<int> oddDigits;
    for (int i = 0; i < digits.size(); i++) {
        if ((i + 1) % 2 == 0) {
            evenDigits.push_back(digits[i]);
        }
        else {
            oddDigits.push_back(digits[i]);
        }
    }
 
    // Calculate the product of even digits and the sum of
    // odd digits
    int evenProduct = 1;
    int oddSum = 0;
    for (int digit : evenDigits) {
        evenProduct *= digit;
    }
    for (int digit : oddDigits) {
        oddSum += digit;
    }
 
    // Check if the even product is divisible by the odd sum
    return evenProduct % oddSum == 0;
}
 
int main()
{
    int N1 = 2157;
    int N2 = 1234;
 
    cout << boolalpha << IsDivisible(N1) << endl; // True
    cout << boolalpha << IsDivisible(N2) << endl; // True
 
    return 0;
}


Java




import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
 
public class DivisibilityChecker {
    public static boolean isDivisible(int N) {
        // Convert N to a list of digits
        List<Integer> digits = new ArrayList<>();
        int tempN = N;
        while (tempN > 0) {
            digits.add(tempN % 10);
            tempN /= 10;
        }
        Collections.reverse(digits);
 
        // Separate even and odd positioned digits
        List<Integer> evenDigits = new ArrayList<>();
        List<Integer> oddDigits = new ArrayList<>();
        for (int i = 0; i < digits.size(); i++) {
            if ((i + 1) % 2 == 0) {
                evenDigits.add(digits.get(i));
            } else {
                oddDigits.add(digits.get(i));
            }
        }
 
        // Calculate the product of even digits and the sum of odd digits
        int evenProduct = 1;
        int oddSum = 0;
        for (int digit : evenDigits) {
            evenProduct *= digit;
        }
        for (int digit : oddDigits) {
            oddSum += digit;
        }
 
        // Check if the even product is divisible by the odd sum
        return evenProduct % oddSum == 0;
    }
 
    public static void main(String[] args) {
        int N1 = 2157;
        int N2 = 1234;
 
        System.out.println(isDivisible(N1)); // True
        System.out.println(isDivisible(N2)); // True
    }
}
 
// This code is contributed by akshitaguprzj3


Python3




def is_divisible(N):
    digits = [int(digit) for digit in str(N)]
    even_digits = digits[::2]
    odd_digits = digits[2::2]
    even_product = 1
    odd_sum = sum(odd_digits)
    for digit in even_digits:
        even_product *= digit
    return even_product % odd_sum == 0
 
 
N1 = 2157
N2 = 1234
 
print(is_divisible(N1)) # True
print(is_divisible(N2)) # True


C#




using System;
using System.Linq;
 
class Program {
    static bool IsDivisible(int N)
    {
        // Convert N to a list of digits
        int[] digits
            = N.ToString()
                  .ToCharArray()
                  .Select(digit =
                              > int.Parse(digit.ToString()))
                  .ToArray();
 
        // Separate even and odd positioned digits
        int[] evenDigits
            = digits
                  .Where((digit, index) =
                             > (index + 1) % 2 == 0)
                  .ToArray();
        int[] oddDigits
            = digits
                  .Where((digit, index) =
                             > (index + 1) % 2 != 0)
                  .ToArray();
 
        // Calculate the product of even digits and the sum
        // of odd digits
        int evenProduct = 1;
        int oddSum = oddDigits.Sum();
        foreach(int digit in evenDigits)
        {
            evenProduct *= digit;
        }
 
        // Check if the even product is divisible by the odd
        // sum
        return evenProduct % oddSum == 0;
    }
 
    static void Main()
    {
        int N1 = 2157;
        int N2 = 1234;
 
        Console.WriteLine(IsDivisible(N1)); // True
        Console.WriteLine(IsDivisible(N2)); // True
    }
}


Javascript




function isDivisible(N) {
    // Convert N to a list of digits
    let digits = [];
    let tempN = N;
    while (tempN > 0) {
        digits.push(tempN % 10);
        tempN = Math.floor(tempN / 10);
    }
    digits.reverse();
 
    // Separate even and odd positioned digits
    let evenDigits = [];
    let oddDigits = [];
    for (let i = 0; i < digits.length; i++) {
        if ((i + 1) % 2 === 0) {
            evenDigits.push(digits[i]);
        } else {
            oddDigits.push(digits[i]);
        }
    }
 
    // Calculate the product of even digits and the sum of odd digits
    let evenProduct = 1;
    let oddSum = 0;
    for (let digit of evenDigits) {
        evenProduct *= digit;
    }
    for (let digit of oddDigits) {
        oddSum += digit;
    }
 
    // Check if the even product is divisible by the odd sum
    return evenProduct % oddSum === 0;
}
 
let N1 = 2157;
let N2 = 1234;
 
console.log(isDivisible(N1)); // true
console.log(isDivisible(N2)); // true
 
// This code is contributed by akshitaguprzj3


Output

True
True







Time Complexity: O(n), where n is the number of digits in the input number.
Space Complexity: O(n)



Last Updated : 29 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads