Given a number N, the task is to check whether the product of digits at even places of a number is divisible by K. If it is divisible, output “YES” otherwise output “NO”.
Examples:
Input: N = 5478, K = 5
Output: YES
Since, 5 * 7 = 35, which is divisible by 5
Input: N = 19270, K = 2
Output: NO
Approach:
- Find product of digits at even places from right to left.
- Then check the divisibility by taking its modulo with ‘K’
- If modulo gives 0, output YES, otherwise output NO
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool productDivisible( int n, int k)
{
int product = 1, position = 1;
while (n > 0) {
if (position % 2 == 0)
product *= n % 10;
n = n / 10;
position++;
}
if (product % k == 0)
return true ;
return false ;
}
int main()
{
int n = 321922;
int k = 3;
if (productDivisible(n, k))
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
Java
class GFG {
static boolean productDivisible( int n, int k) {
int product = 1 , position = 1 ;
while (n > 0 ) {
if (position % 2 == 0 ) {
product *= n % 10 ;
}
n = n / 10 ;
position++;
}
if (product % k == 0 ) {
return true ;
}
return false ;
}
public static void main(String[] args) {
int n = 321922 ;
int k = 3 ;
if (productDivisible(n, k)) {
System.out.println( "YES" );
} else {
System.out.println( "NO" );
}
}
}
|
Python3
def productDivisible(n, k):
product = 1
position = 1
while n > 0 :
if position % 2 = = 0 :
product * = n % 10
n = n / 10
position + = 1
if product % k = = 0 :
return True
return False
n = 321922
k = 3
if productDivisible(n, k) = = True :
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
static bool productDivisible( int n, int k)
{
int product = 1, position = 1;
while (n > 0)
{
if (position % 2 == 0)
product *= n % 10;
n = n / 10;
position++;
}
if (product % k == 0)
return true ;
return false ;
}
public static void Main()
{
int n = 321922;
int k = 3;
if (productDivisible(n, k))
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
|
PHP
<?php
function productDivisible( $n , $k )
{
$product = 1;
$position = 1;
while ( $n > 0)
{
if ( $position % 2 == 0)
$product *= $n % 10;
$n = (int)( $n / 10);
$position ++;
}
if ( $product % $k == 0)
return true;
return false;
}
$n = 321922;
$k = 3;
if (productDivisible( $n , $k ))
echo "YES" ;
else
echo "NO" ;
?>
|
Javascript
<script>
function productDivisible(n, k)
{
var product = 1, position = 1;
while (n > 0) {
if (position % 2 == 0)
product *= n % 10;
n =parseInt(n / 10);
position++;
}
if (product % k == 0)
return true ;
return false ;
}
var n = 321922;
var k = 3;
if (productDivisible(n, k))
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Time Complexity: O(log10n)
Auxiliary Space: O(1)
Method #2:Using string() method:
- Convert the integer to string then traverse the string and Multiply all even indices by storing it in the product.
- If the product is divisible by k then return True else False.
Below is the implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool productDivisible( int n, int k)
{
int product = 1;
string num = to_string(n);
for ( int i = 0; i < num.length(); i++) {
if (i % 2 == 0) {
product = product * (num[i] - '0' );
}
}
if (product % k == 0) {
return true ;
}
else {
return false ;
}
}
int main()
{
int n = 321922;
int k = 3;
if (productDivisible(n, k)) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
}
|
Java
import java.util.*;
class GFG {
static boolean productDivisible( int n, int k)
{
int product = 1 ;
String num = String.valueOf(n);
for ( int i = 0 ; i < num.length(); i++) {
if (i % 2 == 0 ) {
product = product * (num.charAt(i) - '0' );
}
}
if (product % k == 0 ) {
return true ;
}
else {
return false ;
}
}
public static void main(String[] args)
{
int n = 321922 ;
int k = 3 ;
if (productDivisible(n, k)) {
System.out.println( "YES" );
}
else {
System.out.println( "NO" );
}
}
}
|
Python3
def productDivisible(n, k):
product = 1
num = str (n)
for i in range ( len (num)):
if (i % 2 = = 0 ):
product = product * int (num[i])
if product % k = = 0 :
return True
return False
n = 321922
k = 3
if productDivisible(n, k) = = True :
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static bool productDivisible( int n, int k)
{
int product = 1;
string num = Convert.ToString(n);
for ( int i = 0; i < num.Length; i++) {
if (i % 2 == 0) {
product = product * (num[i] - '0' );
}
}
if (product % k == 0) {
return true ;
}
else {
return false ;
}
}
public static void Main( string [] args)
{
int n = 321922;
int k = 3;
if (productDivisible(n, k)) {
Console.WriteLine( "YES" );
}
else {
Console.WriteLine( "NO" );
}
}
}
|
Javascript
<script>
function productDivisible(n, k){
var product = 1 ;
var num = n.toString()
for (let i = 0 ; i < num.length ; i++){
if (i % 2 == 0){
product = product * Number(num[i])
}
}
if (product % k == 0){
return true
}
else {
return false ;
}
}
var n = 321922
var k = 3
if (productDivisible(n, k)){
document.write( "YES" )
}
else {
document.write( "NO" )
}
</script>
|
Output:
YES
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!