Given a number N and numbers of digits in N, the task is to check whether the product of digits at even places of a number is divisible by sum of digits at odd place. If it is divisible, output “TRUE” otherwise output “FALSE”.
Examples:
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
Approach:
- Find product of digits at even places from right to left.
- Find sum of digits at odd places from right to left.
- Then check the divisibility of product by taking it’s modulo with sum
- If modulo gives 0, output TRUE, otherwise output FALSE
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool productSumDivisible( int n, int size)
{
int sum = 0, product = 1;
while (n > 0) {
if (size % 2 == 0) {
product *= n % 10;
}
else {
sum += n % 10;
}
n = n / 10;
size--;
}
if (product % sum == 0)
return true ;
return false ;
}
int main()
{
int n = 1234;
int len = 4;
if (productSumDivisible(n, len))
cout << "TRUE" ;
else
cout << "FALSE" ;
return 0;
}
|
Java
class GFG {
static boolean productSumDivisible( int n, int size)
{
int sum = 0 , product = 1 ;
while (n > 0 ) {
if (size % 2 == 0 ) {
product *= n % 10 ;
}
else {
sum += n % 10 ;
}
n = n / 10 ;
size--;
}
if (product % sum == 0 ) {
return true ;
}
return false ;
}
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" );
}
}
}
|
Python3
def productSumDivisible(n, size):
sum = 0
product = 1
while (n > 0 ) :
if (size % 2 = = 0 ) :
product * = n % 10
else :
sum + = n % 10
n = n / / 10
size - = 1
if (product % sum = = 0 ):
return True
return False
if __name__ = = "__main__" :
n = 1234
len = 4
if (productSumDivisible(n, len )):
print ( "TRUE" )
else :
print ( "FALSE" )
|
C#
using System;
class GFG {
static bool productSumDivisible( int n, int size)
{
int sum = 0, product = 1;
while (n > 0) {
if (size % 2 == 0) {
product *= n % 10;
}
else {
sum += n % 10;
}
n = n / 10;
size--;
}
if (product % sum == 0) {
return true ;
}
return false ;
}
public static void Main()
{
int n = 1234;
int len = 4;
if (productSumDivisible(n, len))
Console.WriteLine( "TRUE" );
else
Console.WriteLine( "FALSE" );
}
}
|
Javascript
<script>
function productSumDivisible(n, size)
{
var sum = 0, product = 1;
while (n > 0) {
if (size % 2 == 0) {
product *= n % 10;
}
else {
sum += n % 10;
}
n = parseInt(n / 10);
size--;
}
if (product % sum == 0)
return true ;
return false ;
}
var n = 1234;
var len = 4;
if (productSumDivisible(n, len))
document.write( "TRUE" );
else
document.write( "FALSE" );
</script>
|
PHP
<?php
function productSumDivisible( $n , $size )
{
$sum = 0; $product = 1;
while ( $n > 0)
{
if ( $size % 2 == 0)
{
$product *= $n % 10;
}
else
{
$sum += $n % 10;
}
$n = $n / 10;
$size --;
}
if ( $product % $sum == 0)
return true;
return false;
}
$n = 1234;
$len = 4;
if (productSumDivisible( $n , $len ))
echo "TRUE" ;
else
echo "FALSE" ;
?>
|
Time Complexity: O(logn)
Auxiliary Space: (1), since no extra space has been taken.
Method #2:Using string() method
Convert the integer to string then traverse the string and perform two operations
- Multiply all odd indices and store them in product
- Add all even indices and store them in sum
- If the product is divisible by sum then return True else False
Below is the implementation:
C++
#include <iostream>
using namespace std;
#include<string>
bool productSumDivisible( int n)
{
int sum = 0;
int product = 1;
string num = to_string(n);
for ( int i = 0 ; i < num.length() ; i++ ) {
if (i % 2 != 0){
product = product*(num[i]);
}
else {
sum = sum+ int (num[i]);
}
}
if (product % sum == 0){
return true ;
}
else {
return false ;
}
}
int main()
{
int n = 1234;
if (productSumDivisible(n))
{
cout<< "true" ;
}
else {
cout<< "false" ;
}
}
|
Java
import java.io.*;
class GFG {
static boolean productSumDivisible( int n)
{
int sum = 0 ;
int product = 1 ;
String num = String.valueOf(n);
for ( int i = 0 ; i < num.length() ; i++ ) {
if (i % 2 != 0 ){
product = product*Character.getNumericValue(num.charAt(i));
}
else {
sum = sum+Character.getNumericValue(num.charAt(i));
}
}
if (product % sum == 0 ){
return true ;
}
else {
return false ;
}
}
public static void main (String[] args) {
int n = 1234 ;
if (productSumDivisible(n))
{
System.out.println( "TRUE" );
}
else {
System.out.println( "FALSE" );
}
}
}
|
Python3
def productSumDivisible(n):
sum = 0
product = 1
num = str (n)
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
if __name__ = = "__main__" :
n = 1234
if (productSumDivisible(n)):
print ( "TRUE" )
else :
print ( "FALSE" )
|
C#
using System;
class GFG{
static bool productSumDivisible( int n)
{
int sum = 0;
int product = 1;
string num = n.ToString();
for ( int i = 0; i < num.Length; i++ )
{
if (i % 2 != 0)
{
product = product*( int )Char.GetNumericValue(num[i]);
}
else
{
sum = sum+( int )Char.GetNumericValue(num[i]);
}
}
if (product % sum == 0)
{
return true ;
}
else
{
return false ;
}
}
static public void Main()
{
int n = 1234;
if (productSumDivisible(n))
{
Console.WriteLine( "TRUE" );
}
else
{
Console.WriteLine( "FALSE" );
}
}
}
|
Javascript
<script>
function productSumDivisible(n){
var sum = 0
var product = 1
var num = n.toString();
for (i = 0 ; i < num.length ; i++ ) {
if (i % 2 != 0){
product = product*Number(num[i])
}
else {
sum = sum+Number(num[i])
}
}
if (product % sum == 0){
return true ;
}
else {
return false ;
}
}
var n = 1234
if (productSumDivisible(n)){
document.write( "TRUE" )
}
else {
document.write( "FALSE" )
}
</script>
|
Time Complexity: O(d), where d is the number of digits in n
Auxiliary Space: (1), since no extra space has been taken.
Using List Comprehension:
Approach:
This approach uses list comprehension to extract the even and odd digits from the input number. It then computes the product of the even digits and the sum of the odd digits and checks if the product of even digits is divisible by the sum of odd digits.
Convert the input integer N into a list of its digits by converting it to a string, iterating over the string, and converting each character back to an integer using the int() function. This is done using the expression digits = [int(digit) for digit in str(N)].
Extract the even-indexed digits (i.e., the digits at positions 0, 2, 4, etc.) into a separate list even_digits using slicing. This is done using the expression even_digits = digits[::2].
Extract the odd-indexed digits (i.e., the digits at positions 1, 3, 5, etc.) into a separate list odd_digits using slicing. This is done using the expression odd_digits = digits[1::2].
Initialize a variable even_product to 1, which will hold the product of the even digits.
Compute the sum of the odd digits using the sum() function and store it in the variable odd_sum.
Iterate over the even digits using a for loop and multiply them together to compute their product, which is stored in the variable even_product. This is done using the code for digit in even_digits: even_product *= digit.
Test whether the even product is divisible by the odd sum using the modulo operator %. If the remainder is 0, then the even product is divisible by the odd sum and the function returns True. Otherwise, the even product is not divisible by the odd sum and the function returns False. This is done using the expression return even_product % odd_sum == 0.
C++
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool IsDivisible( int N)
{
vector< int > digits;
int tempN = N;
while (tempN > 0) {
digits.push_back(tempN % 10);
tempN /= 10;
}
reverse(digits.begin(), digits.end());
vector< int > evenDigits;
vector< int > oddDigits;
for ( int i = 0; i < digits.size(); i++) {
if ((i + 1) % 2 == 0) {
evenDigits.push_back(digits[i]);
}
else {
oddDigits.push_back(digits[i]);
}
}
int evenProduct = 1;
int oddSum = 0;
for ( int digit : evenDigits) {
evenProduct *= digit;
}
for ( int digit : oddDigits) {
oddSum += digit;
}
return evenProduct % oddSum == 0;
}
int main()
{
int N1 = 2157;
int N2 = 1234;
cout << boolalpha << IsDivisible(N1) << endl;
cout << boolalpha << IsDivisible(N2) << endl;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
public class DivisibilityChecker {
public static boolean isDivisible( int N) {
List<Integer> digits = new ArrayList<>();
int tempN = N;
while (tempN > 0 ) {
digits.add(tempN % 10 );
tempN /= 10 ;
}
Collections.reverse(digits);
List<Integer> evenDigits = new ArrayList<>();
List<Integer> oddDigits = new ArrayList<>();
for ( int i = 0 ; i < digits.size(); i++) {
if ((i + 1 ) % 2 == 0 ) {
evenDigits.add(digits.get(i));
} else {
oddDigits.add(digits.get(i));
}
}
int evenProduct = 1 ;
int oddSum = 0 ;
for ( int digit : evenDigits) {
evenProduct *= digit;
}
for ( int digit : oddDigits) {
oddSum += digit;
}
return evenProduct % oddSum == 0 ;
}
public static void main(String[] args) {
int N1 = 2157 ;
int N2 = 1234 ;
System.out.println(isDivisible(N1));
System.out.println(isDivisible(N2));
}
}
|
Python3
def is_divisible(N):
digits = [ int (digit) for digit in str (N)]
even_digits = digits[:: 2 ]
odd_digits = digits[ 2 :: 2 ]
even_product = 1
odd_sum = sum (odd_digits)
for digit in even_digits:
even_product * = digit
return even_product % odd_sum = = 0
N1 = 2157
N2 = 1234
print (is_divisible(N1))
print (is_divisible(N2))
|
C#
using System;
using System.Linq;
class Program {
static bool IsDivisible( int N)
{
int [] digits
= N.ToString()
.ToCharArray()
.Select(digit =
> int .Parse(digit.ToString()))
.ToArray();
int [] evenDigits
= digits
.Where((digit, index) =
> (index + 1) % 2 == 0)
.ToArray();
int [] oddDigits
= digits
.Where((digit, index) =
> (index + 1) % 2 != 0)
.ToArray();
int evenProduct = 1;
int oddSum = oddDigits.Sum();
foreach ( int digit in evenDigits)
{
evenProduct *= digit;
}
return evenProduct % oddSum == 0;
}
static void Main()
{
int N1 = 2157;
int N2 = 1234;
Console.WriteLine(IsDivisible(N1));
Console.WriteLine(IsDivisible(N2));
}
}
|
Javascript
function isDivisible(N) {
let digits = [];
let tempN = N;
while (tempN > 0) {
digits.push(tempN % 10);
tempN = Math.floor(tempN / 10);
}
digits.reverse();
let evenDigits = [];
let oddDigits = [];
for (let i = 0; i < digits.length; i++) {
if ((i + 1) % 2 === 0) {
evenDigits.push(digits[i]);
} else {
oddDigits.push(digits[i]);
}
}
let evenProduct = 1;
let oddSum = 0;
for (let digit of evenDigits) {
evenProduct *= digit;
}
for (let digit of oddDigits) {
oddSum += digit;
}
return evenProduct % oddSum === 0;
}
let N1 = 2157;
let N2 = 1234;
console.log(isDivisible(N1));
console.log(isDivisible(N2));
|
Time Complexity: O(n), where n is the number of digits in the input number.
Space Complexity: O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!