给定数字N和N中的数字位数,任务是检查是否可以用奇数个位数的总和除掉数字偶数个位数的乘积。如果可以整除,则输出“ TRUE”,否则输出“ FALSE”。
例子:
Input: N = 2157
Output: TRUE
Since, 1 * 7 = 7, which is divisible by 2+5=7
Input: N = 1234
Output: TRUE
Since, 2 * 4 = 8, which is divisible by 1 + 3 = 4
方法:
- 在从右到左的偶数位置查找数字的乘积。
- 从右到左找到奇数位的数字总和。
- 然后通过对乘积求模来检查乘积的可除性
- 如果取模为0,则输出TRUE,否则输出FALSE
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// below function checks whether
// product of digits at even places
// is divisible by sum of digits at odd places
bool productSumDivisible(int n, int size)
{
int sum = 0, product = 1;
while (n > 0) {
// if size is even
if (size % 2 == 0) {
product *= n % 10;
}
// if size is odd
else {
sum += n % 10;
}
n = n / 10;
size--;
}
if (product % sum == 0)
return true;
return false;
}
// Driver code
int main()
{
int n = 1234;
int len = 4;
if (productSumDivisible(n, len))
cout << "TRUE";
else
cout << "FALSE";
return 0;
}
Java
// JAVA implementation of the above approach
class GFG {
// below function checks whether
// product of digits at even places
// is divisible by sum of digits at odd places
static boolean productSumDivisible(int n, int size)
{
int sum = 0, product = 1;
while (n > 0) {
// if size is even
if (size % 2 == 0) {
product *= n % 10;
}
// if size is odd
else {
sum += n % 10;
}
n = n / 10;
size--;
}
if (product % sum == 0) {
return true;
}
return false;
}
// Driver code
public static void main(String[] args)
{
int n = 1234;
int len = 4;
if (productSumDivisible(n, len)) {
System.out.println("TRUE");
}
else {
System.out.println("FALSE");
}
}
}
Python 3
# Python 3 implementation of the above approach
# Below function checks whether product
# of digits at even places is divisible
# by sum of digits at odd places
def productSumDivisible(n, size):
sum = 0
product = 1
while (n > 0) :
# if size is even
if (size % 2 == 0) :
product *= n % 10
# if size is odd
else :
sum += n % 10
n = n // 10
size -= 1
if (product % sum == 0):
return True
return False
# Driver code
if __name__ == "__main__":
n = 1234
len = 4
if (productSumDivisible(n, len)):
print("TRUE")
else :
print("FALSE")
# This code is contributed by ChitraNayal
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 productSumDivisible(int n, int size)
{
int sum = 0, product = 1;
while (n > 0) {
// if size is even
if (size % 2 == 0) {
product *= n % 10;
}
// if size is odd
else {
sum += n % 10;
}
n = n / 10;
size--;
}
if (product % sum == 0) {
return true;
}
return false;
}
// Driver code
public static void Main()
{
int n = 1234;
int len = 4;
if (productSumDivisible(n, len))
Console.WriteLine("TRUE");
else
Console.WriteLine("FALSE");
}
}
PHP
0)
{
// if size is even
if ($size % 2 == 0)
{
$product *= $n % 10;
}
// if size is odd
else
{
$sum += $n % 10;
}
$n = $n / 10;
$size--;
}
if ($product % $sum == 0)
return true;
return false;
}
// Driver code
$n = 1234;
$len = 4;
if (productSumDivisible($n, $len))
echo "TRUE";
else
echo "FALSE";
// This code is contributed by anuj_67..
?>
Javascript
Python3
# Python 3 implementation of the above approach
# Below function checks whether product
# of digits at even places is divisible
# by sum of digits at odd places
def productSumDivisible(n):
sum = 0
product = 1
# Converting integer to string
num = str(n)
# Traveersing the string
for i in range(len(num)):
if(i % 2 != 0):
product = product*int(num[i])
else:
sum = sum+int(num[i])
if (product % sum == 0):
return True
return False
# Driver code
if __name__ == "__main__":
n = 1234
if (productSumDivisible(n)):
print("TRUE")
else:
print("FALSE")
# This code is contributed by vikkycirus
输出:
TRUE
方法2:使用字符串()方法
将整数转换为字符串,然后遍历字符串并执行两个操作
- 将所有奇数索引相乘并将其存储在产品中
- 将所有偶数索引相加并将其总和存储
- 如果乘积可被总和整除,则返回True否则为False
下面是实现:
Python3
# Python 3 implementation of the above approach
# Below function checks whether product
# of digits at even places is divisible
# by sum of digits at odd places
def productSumDivisible(n):
sum = 0
product = 1
# Converting integer to string
num = str(n)
# Traveersing the string
for i in range(len(num)):
if(i % 2 != 0):
product = product*int(num[i])
else:
sum = sum+int(num[i])
if (product % sum == 0):
return True
return False
# Driver code
if __name__ == "__main__":
n = 1234
if (productSumDivisible(n)):
print("TRUE")
else:
print("FALSE")
# This code is contributed by vikkycirus
输出:
TRUE