Comprobar si el producto de dígitos en lugares pares es divisible por la suma de dígitos en lugares impares de un número

Dado un número N y números de dígitos en N, la tarea es verificar si el producto de dígitos en lugares pares de un número es divisible por la suma de dígitos en lugares impares. Si es divisible, emite «VERDADERO»; de lo contrario, emite «FALSO». 

Ejemplos: 

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   

Acercarse:  

  1. Encuentra el producto de dígitos en lugares pares de derecha a izquierda.
  2. Encuentra la suma de dígitos en lugares impares de derecha a izquierda.
  3. Luego verifique la divisibilidad del producto tomando su módulo con suma
  4. Si el módulo da 0, emite VERDADERO, de lo contrario, emite FALSO

A continuación se muestra la implementación del enfoque anterior:  

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

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

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>
Producción: 

TRUE

 

Complejidad de tiempo: O (logn)

Espacio Auxiliar: (1), ya que no se ha tomado ningún espacio extra.

Método #2: Usar el método string()

Convierta el entero en una string, luego recorra la string y realice dos operaciones

  • Multiplique todos los índices impares y guárdelos en el producto
  • Agregue todos los índices pares y guárdelos en suma
  • Si el producto es divisible por la suma, devuelve Verdadero de lo contrario Falso

A continuación se muestra la implementación:

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);
 
    // Traveersing 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);
 
    // Traveersing 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)
     
    # Traveersing 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();
     
    // Traveersing 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();
      
    // Traveersing 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>

Producción:

TRUE

Complejidad temporal: O(d), donde d es el número de dígitos en n

Espacio Auxiliar: (1), ya que no se ha tomado ningún espacio extra.

Publicación traducida automáticamente

Artículo escrito por 29AjayKumar y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *