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 = 8Input: N = 4324
Output: No
Product of digits at odd places = 4 * 2 = 8
Product of digits at even places = 3 * 4 = 12
Approach:
- Find product of digits at even places and store it in prodEven.
- Find 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++
// 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 // 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 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 = 4324if 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
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
?>
No
Recommended Posts:
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Check whether product of digits at even places of a number is divisible by K
- Check whether sum of digits at odd places of a number is divisible by K
- Smallest number k such that the product of digits of k is equal to n
- Find the sum of digits of a number at even and odd places
- Primality test for the sum of digits at odd places of a number
- Find maximum product of digits among numbers less than or equal to N
- Check if the sum of distinct digits of two integers are equal
- Check if the array has an element which is equal to product of remaining elements
- Number of digits in the product of two numbers
- Count number of triplets with product equal to given number with duplicates allowed
- Maximum sum and product of the M consecutive digits in a number
- Program to calculate product of digits of a number
- Sum of digits equal to a given number in PL/SQL
- Sum and Product of digits in a number that divide the number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



