Check if product of digits of a number at even and odd places is equal
Given an integer N, the task is to check whether the product of digits at even and odd places of a number are equal. If they are equal, print Yes otherwise print No.
Examples:
Input: N = 2841
Output: Yes
Product of digits at odd places = 2 * 4 = 8
Product of digits at even places = 8 * 1 = 8
Input: N = 4324
Output: No
Product of digits at odd places = 4 * 2 = 8
Product of digits at even places = 3 * 4 = 12
Approach:
- Find the product of digits at even places and store it in prodEven.
- Find the product of digits at odd places and store it in prodOdd.
- If prodEven = prodOdd then print Yes otherwise print No.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool productEqual( int n)
{
if (n < 10)
return false ;
int prodOdd = 1, prodEven = 1;
while (n > 0) {
int digit = n % 10;
prodEven *= digit;
n /= 10;
digit = n % 10;
prodOdd *= digit;
n /= 10;
}
if (prodEven == prodOdd)
return true ;
return false ;
}
int main()
{
int n = 4324;
if (productEqual(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG {
static boolean productEqual( int n)
{
if (n < 10 )
return false ;
int prodOdd = 1 , prodEven = 1 ;
while (n > 0 ) {
int digit = n % 10 ;
prodOdd *= digit;
n /= 10 ;
if (n == 0 )
break ;
digit = n % 10 ;
prodEven *= digit;
n /= 10 ;
}
if (prodEven == prodOdd)
return true ;
return false ;
}
public static void main(String args[])
{
int n = 4324 ;
if (productEqual(n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def productEqual(n):
if n < 10 :
return False
prodOdd = 1
prodEven = 1
while n > 0 :
digit = n % 10
prodOdd * = digit
n = n / / 10
if n = = 0 :
break
digit = n % 10
prodEven * = digit
n = n / / 10
if prodOdd = = prodEven:
return True
return False
n = 4324
if productEqual(n):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static bool productEqual( int n)
{
if (n < 10)
return false ;
int prodOdd = 1, prodEven = 1;
while (n > 0) {
int digit = n % 10;
prodOdd *= digit;
n /= 10;
if (n == 0)
break ;
digit = n % 10;
prodEven *= digit;
n /= 10;
}
if (prodEven == prodOdd)
return true ;
return false ;
}
static void Main()
{
int n = 4324;
if (productEqual(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function productEqual( $n )
{
if ( $n < 10)
return false;
$prodOdd = 1;
$prodEven = 1;
while ( $n > 0)
{
$digit = $n % 10;
$prodOdd *= $digit ;
$n /= 10;
if ( $n == 0)
break ;
$digit = $n % 10;
$prodEven *= $digit ;
$n /= 10;
}
if ( $prodEven == $prodOdd )
return true;
return false;
}
$n = 4324;
if (productEqual(! $n ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function productEqual(n)
{
if (n < 10)
return false ;
let prodOdd = 1, prodEven = 1;
while (n > 0) {
let digit = n % 10;
prodOdd *= digit;
n = Math.floor(n / 10);
if (n == 0)
break ;
digit = n % 10;
prodEven *= digit;
n = Math.floor(n / 10);
}
if (prodEven == prodOdd)
return true ;
return false ;
}
let n = 4324;
if (productEqual(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time complexity: O(log10n)
Auxiliary Space: O(1), since no extra space has been taken.
Method #2: Converting Integer to String:
- Convert the integer to string. Traverse the string and store all even indices’ products in one variable and all odd indices’ products in another variable.
- If both are equal then print Yes else No
Below is the implementation:
C++
#include <iostream>
using namespace std;
void getResult( int n)
{
int proOdd = 1;
int proEven = 1;
string num = to_string(n);
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" ;
}
int main()
{
int n = 4324;
getResult(n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void getResult( int n)
{
int proOdd = 1 ;
int proEven = 1 ;
String num = String.valueOf(n);
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" );
}
public static void main(String[] args)
{
int n = 4324 ;
getResult(n);
}
}
|
Python3
def getResult(n):
proOdd = 1
proEven = 1
num = str (n)
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" )
if __name__ = = "__main__" :
n = 4324
getResult(n)
|
C#
using System;
public class GFG{
static void getResult( int n)
{
int proOdd = 1;
int proEven = 1;
String num = String.Join( "" ,n);
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" );
}
public static void Main(String[] args)
{
int n = 4324;
getResult(n);
}
}
|
Javascript
<script>
function getResult(n)
{
let proOdd = 1;
let proEven = 1;
let num = n.toString();
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>
|
Output:
No
Time complexity: O(d), where d is the number of digits in the integer.
Auxiliary Space: O(1)
Last Updated :
22 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...