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

Dado un número N, la tarea es verificar si el producto de los dígitos en los lugares pares de un número es divisible por K. Si es divisible, emita «SÍ», de lo contrario, emita «NO». 
Ejemplos: 

Input: N = 5478, K = 5
Output: YES
Since, 5 * 7 = 35, which is divisible by 5

Input: N = 19270, K = 2
Output: NO 

Acercarse:  

  1. Encuentra el producto de dígitos en lugares pares de derecha a izquierda.
  2. Luego verifica la divisibilidad tomando su módulo con ‘K’
  3. Si módulo da 0, salida SÍ, de lo contrario, salida NO

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 K
bool productDivisible(int n, int k)
{
    int product = 1, position = 1;
    while (n > 0) {
 
        // if position is even
        if (position % 2 == 0)
            product *= n % 10;
        n = n / 10;
        position++;
    }
 
    if (product % k == 0)
        return true;
    return false;
}
 
// Driver code
int main()
{
    int n = 321922;
    int k = 3;
 
    if (productDivisible(n, k))
        cout << "YES";
    else
        cout << "NO";
 
    return 0;
}

Java

// JAVA implementation of the above approach
class GFG {
// below function checks whether
// product of digits at even places
// is divisible by K
 
    static boolean productDivisible(int n, int k) {
        int product = 1, position = 1;
        while (n > 0) {
 
            // if position is even
            if (position % 2 == 0) {
                product *= n % 10;
            }
            n = n / 10;
            position++;
        }
 
        if (product % k == 0) {
            return true;
        }
        return false;
    }
 
// Driver code
    public static void main(String[] args) {
        int n = 321922;
        int k = 3;
 
        if (productDivisible(n, k)) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
    }
}

Python3

# Python3 implementation of the
# above approach
 
# below function checks whether
# product of digits at even places
# is divisible by K
def productDivisible(n, k):
    product = 1
    position = 1
    while n > 0:
         
        # if position is even
        if position % 2 == 0:
            product *= n % 10
        n = n / 10
        position += 1
    if product % k == 0:
        return True
    return False
 
# Driver code
n = 321922
k = 3
if productDivisible(n, k) == True:
    print("YES")
else:
    print("NO")
 
# This code is contributed
# by Shrikant13

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 productDivisible(int n, int k)
{
    int product = 1, position = 1;
    while (n > 0)
    {
 
        // if position is even
        if (position % 2 == 0)
            product *= n % 10;
        n = n / 10;
        position++;
    }
 
    if (product % k == 0)
        return true;
    return false;
}
 
// Driver code
public static void Main()
{
    int n = 321922;
    int k = 3;
 
    if (productDivisible(n, k))
        Console.WriteLine("YES");
    else
        Console.WriteLine("NO");
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)

PHP

<?php
// PHP implementation of the
// above approach
 
// Below function checks whether
// product of digits at even places
// is divisible by K
function productDivisible($n, $k)
{
    $product = 1;
    $position = 1;
    while ($n > 0)
    {
 
        // if position is even
        if ($position % 2 == 0)
            $product *= $n % 10;
        $n = (int)($n / 10);
        $position++;
    }
 
    if ($product % $k == 0)
        return true;
    return false;
}
 
// Driver code
$n = 321922;
$k = 3;
 
if (productDivisible($n, $k))
    echo "YES";
else
    echo "NO";
 
// This code is contributed by mits
?>

Javascript

<script>
 
// Javascript implementation of the above approach
 
// below function checks whether
// product of digits at even places
// is divisible by K
function productDivisible(n, k)
{
    var product = 1, position = 1;
    while (n > 0) {
 
        // if position is even
        if (position % 2 == 0)
            product *= n % 10;
        n =parseInt(n / 10);
        position++;
    }
 
    if (product % k == 0)
        return true;
    return false;
}
 
// Driver code
var n = 321922;
var k = 3;
if (productDivisible(n, k))
    document.write( "YES");
else
    document.write( "NO");
 
</script>
Producción: 

YES

 

Complejidad de tiempo: O (log 10 n)

Espacio Auxiliar: O(1)

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

  1. Convierta el número entero en una string, luego recorra la string y multiplique todos los índices pares almacenándolos en el producto.
  2. Si el producto es divisible por k, devuelva Verdadero, de lo contrario, Falso.

A continuación se muestra la implementación:

Python3

# Python3 implementation of the
# above approach
 
# Function checks whether
# product of digits at even places
# is divisible by K
def productDivisible(n, k):
    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])
 
    if product % k == 0:
        return True
    return False
 
 
# Driver code
n = 321922
k = 3
if productDivisible(n, k) == True:
    print("YES")
else:
    print("NO")
 
# This code is contributed by vikkycirus

Javascript

<script>
 
// JavaScript implementation of the
// above approach
  
// Function checks whether
// product of digits at even places
// is divisible by K
function productDivisible(n, k){
    var product = 1 ;
      
    // Converting integer to string
    var num = n.toString()
      
    // Traversing the string
    for(let i = 0 ; i < num.length ; i++){
        if(i % 2 == 0){
            product = product * Number(num[i])
        }
    }
    if (product % k == 0){
        return true
    }
    else{
    return false;
    }
}
  
  
// Driver code
var n = 321922
var k = 3
if(productDivisible(n, k)){
    document.write("YES")
}
else{
    document.write("NO")
}
 
 
</script>

Producción:

YES

Publicación traducida automáticamente

Artículo escrito por Shashank_Sharma 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 *