给定整数N ,任务是检查数字的偶数和奇数位的数字乘积是否相等。如果相等,则打印“是”,否则打印“否” 。
例子:
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
方法:
- 在偶数位置查找数字乘积并将其存储在prodEven中。
- 在奇数位找到数字乘积并将其存储在prodOdd中。
- 如果prodEven = prodOdd,则打印“是”,否则打印“否” 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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 = 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
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
?>
C++
// C++ implementation of the approach
#include
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
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
输出:
No
方法2:使用字符串()方法:
- 将整数转换为字符串。遍历字符串并将所有偶数索引乘积存储在一个变量中,将所有奇数索引乘积存储在另一个变量中。
- 如果两者相等,则打印是,否则,否
下面是实现:
C++
// C++ implementation of the approach
#include
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
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
输出:
No