Comprobar si el producto de los dígitos de un número en lugares pares e impares es igual

Dado un número entero N , la tarea es verificar si el producto de los dígitos en los lugares pares e impares de un número son iguales. Si son iguales, escriba , de lo contrario , escriba No.

Ejemplos: 

Entrada: N = 2841 
Salida: Sí 
Producto de dígitos en lugares impares = 2 * 4 = 8 
Producto de dígitos en lugares pares = 8 * 1 = 8

Entrada: N = 4324 
Salida: No 
Producto de dígitos en lugares impares = 4 * 2 = 8 
Producto de dígitos en lugares pares = 3 * 4 = 12   

Acercarse:  

  • Encuentre el producto de dígitos en lugares pares y guárdelo en prodEven .
  • Encuentre el producto de dígitos en lugares impares y guárdelo en prodOdd .
  • Si prodEven = prodOdd , imprima ; de lo contrario, imprima No.

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

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function that returns true if the product
// of even positioned digits is equal to
// the product of odd positioned digits in n
bool productEqual(int n)
{
 
    // If n is a single digit number
    if (n < 10)
        return false;
    int prodOdd = 1, prodEven = 1;
 
    while (n > 0) {
 
        // Take two consecutive digits
        // at a time
        // last digit
        int digit = n % 10;
        prodEven *= digit;
        n /= 10;
 
        // Second last digit
        digit = n % 10;
        prodOdd *= digit;
        n /= 10;
    }
 
    // If the products are equal
    if (prodEven == prodOdd)
        return true;
 
    // If products are not equal
    return false;
}
 
// Driver code
int main()
{
    int n = 4324;
 
    if (productEqual(n))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}

Java

// Java implementation of the approach
 
class GFG {
 
    // Function that returns true
    // if the product of even positioned
    // digits is equal to the product of
    // odd positioned digits in n
    static boolean productEqual(int n)
    {
 
        // If n is a single digit number
        if (n < 10)
            return false;
        int prodOdd = 1, prodEven = 1;
 
        while (n > 0) {
 
            // Take two consecutive digits
            // at a time
            // First digit
            int digit = n % 10;
            prodOdd *= digit;
            n /= 10;
 
            // If n becomes 0 then
            // there's no more digit
            if (n == 0)
                break;
 
            // Second digit
            digit = n % 10;
            prodEven *= digit;
            n /= 10;
        }
 
        // If the products are equal
        if (prodEven == prodOdd)
            return true;
 
        // If products are not equal
        return false;
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 4324;
 
        if (productEqual(n))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
    // This code is contributed by Ryuga
}

Python3

# Python implementation of the approach
 
# Function that returns true if the product
# of even positioned digits is equal to
# the product of odd positioned digits in n
 
 
def productEqual(n):
    if n < 10:
        return False
    prodOdd = 1
    prodEven = 1
 
    # Take two consecutive digits
    # at a time
    # First digit
    while n > 0:
        digit = n % 10
        prodOdd *= digit
        n = n//10
 
        # If n becomes 0 then
        # there's no more digit
        if n == 0:
            break
        digit = n % 10
        prodEven *= digit
        n = n//10
 
    # If the products are equal
    if prodOdd == prodEven:
        return True
 
    # If the products are not equal
    return False
 
 
# Driver code
n = 4324
if productEqual(n):
    print("Yes")
else:
    print("No")
 
# This code is contributed by Shrikant13

C#

// C# implementation of the approach
using System;
 
class GFG {
 
    // Function that returns true
    // if the product of even positioned
    // digits is equal to the product of
    // odd positioned digits in n
    static bool productEqual(int n)
    {
 
        // If n is a single digit number
        if (n < 10)
            return false;
        int prodOdd = 1, prodEven = 1;
 
        while (n > 0) {
 
            // Take two consecutive digits
            // at a time
            // First digit
            int digit = n % 10;
            prodOdd *= digit;
            n /= 10;
 
            // If n becomes 0 then
            // there's no more digit
            if (n == 0)
                break;
 
            // Second digit
            digit = n % 10;
            prodEven *= digit;
            n /= 10;
        }
 
        // If the products are equal
        if (prodEven == prodOdd)
            return true;
 
        // If products are not equal
        return false;
    }
 
    // Driver code
    static void Main()
    {
        int n = 4324;
 
        if (productEqual(n))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by mits

PHP

<?php
// PHP implementation of the approach
 
// Function that returns true if the product
// of even positioned digits is equal to
// the product of odd positioned digits in n
function productEqual($n)
{
 
    // If n is a single digit number
    if ($n < 10)
        return false;
    $prodOdd = 1;
    $prodEven = 1;
 
    while ($n > 0)
    {
 
        // Take two consecutive digits
        // at a time
         
        // First digit
        $digit = $n % 10;
        $prodOdd *= $digit;
        $n /= 10;
 
        // If n becomes 0 then
        // there's no more digit
        if ($n == 0)
            break;
 
        // Second digit
        $digit = $n % 10;
        $prodEven *= $digit;
        $n /= 10;
    }
 
    // If the products are equal
    if ($prodEven == $prodOdd)
        return true;
 
    // If products are not equal
    return false;
}
 
// Driver code
$n = 4324;
if (productEqual(!$n))
    echo "Yes";
else
    echo "No";
 
// This code is contributed by jit_t
?>

Javascript

<script>
 
// JavaScript implementation of the approach
 
// Function that returns true if the product
// of even positioned digits is equal to
// the product of odd positioned digits in n
function productEqual(n)
{
 
    // If n is a single digit number
    if (n < 10)
        return false;
    let prodOdd = 1, prodEven = 1;
 
    while (n > 0) {
 
        // Take two consecutive digits
        // at a time
        // First digit
        let digit = n % 10;
        prodOdd *= digit;
        n = Math.floor(n / 10);
 
        // If n becomes 0 then
        // there's no more digit
        if (n == 0)
            break;
 
        // Second digit
        digit = n % 10;
        prodEven *= digit;
        n = Math.floor(n / 10);
    }
 
    // If the products are equal
    if (prodEven == prodOdd)
        return true;
 
    // If products are not equal
    return false;
}
 
// Driver code
 
    let n = 4324;
 
    if (productEqual(n))
        document.write("Yes");
    else
        document.write("No");
 
 
// This code is contributed by Surbhi Tyagi.
 
</script>
Producción: 

No

 

Complejidad del tiempo: O(log 10 n)

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

Método n.º 2: convertir enteros en strings:

  1. Convierta el entero en string. Recorra la string y almacene todos los productos de índices pares en una variable y todos los productos de índices impares en otra variable.
  2. Si ambos son iguales, imprima Sí, de lo contrario, No

A continuación se muestra la implementación:

C++

// C++ implementation of the approach
#include <iostream>
using namespace std;
 
void getResult(int n)
{
     
    // To store the respective product
    int proOdd = 1;
    int proEven = 1;
 
    // Converting integer to string
    string num = to_string(n);
 
    // Traversing the string
    for(int i = 0; i < num.size(); i++)
        if (i % 2 == 0)
            proOdd = proOdd * (num[i] - '0');
        else
            proEven = proEven * (num[i] - '0');
 
    if (proOdd == proEven)
        cout << "Yes";
    else
        cout << "No";
}
 
// Driver code
int main()
{
    int n = 4324;
     
    getResult(n);
     
    return 0;
}
 
// This code is contributed by sudhanshugupta2019a

Java

// Java implementation of the approach
 
import java.util.*;
 
class GFG{
 
static void getResult(int n)
{
     
    // To store the respective product
    int proOdd = 1;
    int proEven = 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)
            proOdd = proOdd * (num.charAt(i) - '0');
        else
            proEven = proEven * (num.charAt(i) - '0');
 
    if (proOdd == proEven)
        System.out.print("Yes");
    else
        System.out.print("No");
}
 
// Driver code
public static void main(String[] args)
{
    int n = 4324;
     
    getResult(n);
     
}
}
 
// This code is contributed by 29AjayKumar

Python3

# Python3 implementation of the approach
 
def getResult(n):
 
    # To store the respective product
    proOdd = 1
    proEven = 1
     
    # Converting integer to string
    num = str(n)
     
    # Traversing the string
    for i in range(len(num)):
        if(i % 2 == 0):
            proOdd = proOdd*int(num[i])
        else:
            proEven = proEven*int(num[i])
 
    if(proOdd == proEven):
        print("Yes")
    else:
        print("No")
 
 
# Driver code
if __name__ == "__main__":
    n = 4324
    getResult(n)
 
# This code is contributed by vikkycirus

C#

// C# implementation of the approach
using System;
public class GFG{
 
static void getResult(int n)
{
     
    // To store the respective product
    int proOdd = 1;
    int proEven = 1;
 
    // Converting integer to String
    String num = String.Join("",n);
 
    // Traversing the String
    for(int i = 0; i < num.Length; i++)
        if (i % 2 == 0)
            proOdd = proOdd * (num[i] - '0');
        else
            proEven = proEven * (num[i] - '0');
 
    if (proOdd == proEven)
        Console.Write("Yes");
    else
        Console.Write("No");
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 4324;
    getResult(n);
}
}
 
// This code is contributed by 29AjayKumar

Javascript

<script>
    // Javascript implementation of the approach
     
    function getResult(n)
    {
 
        // To store the respective product
        let proOdd = 1;
        let proEven = 1;
 
        // Converting integer to String
        let num = n.toString();
 
        // Traversing the String
        for(let i = 0; i < num.length; i++)
            if (i % 2 == 0)
                proOdd = proOdd * (num[i].charCodeAt() - '0'.charCodeAt());
            else
                proEven = proEven * (num[i].charCodeAt() - '0'.charCodeAt());
 
        if (proOdd == proEven)
            document.write("Yes");
        else
            document.write("No");
    }
     
    let n = 4324;
      
    getResult(n);
 
</script>

Producción:

No

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

Espacio Auxiliar: O(1)

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 *