Check whether sum of digits at odd places of a number is divisible by K
Last Updated :
23 Jul, 2022
Given two integers ‘N’ and ‘K’, the task is to find the sum of digits of ‘N’ at its odd places (right to left) and check whether the sum is divisible by ‘K’. If it is divisible, output YES, otherwise output NO.
Examples:
Input: N = 4325, K = 4
Output: YES
Since, 3 + 5 = 8, which is divisible by 4.
Input: N = 1209, K = 3
Output: NO
Approach:
- Find the sum of the digits of ‘N’ at odd places (right to left).
- Then check the divisibility of the sum by taking its modulo with ‘K’.
- If it is divisible, then output ‘YES’, otherwise output ‘NO’.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool SumDivisible( int n, int k)
{
int sum = 0, position = 1;
while (n > 0) {
if (position % 2 == 1)
sum += n % 10;
n = n / 10;
position++;
}
if (sum % k == 0)
return true ;
return false ;
}
int main()
{
int n = 592452;
int k = 3;
if (SumDivisible(n, k))
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
Java
import java.util.*;
class solution
{
static boolean SumDivisible( int n, int k)
{
int sum = 0 , position = 1 ;
while (n > 0 ) {
if (position % 2 == 1 )
sum += n % 10 ;
n = n / 10 ;
position++;
}
if (sum % k == 0 )
return true ;
return false ;
}
public static void main(String arr[])
{
int n = 592452 ;
int k = 3 ;
if (SumDivisible(n, k))
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python 3
def SumDivisible(n, k):
sum = 0
position = 1
while (n > 0 ) :
if (position % 2 = = 1 ):
sum + = n % 10
n = n / / 10
position + = 1
if ( sum % k = = 0 ):
return True
return False
if __name__ = = "__main__" :
n = 592452
k = 3
if (SumDivisible(n, k)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
static bool SumDivisible( int n, int k)
{
int sum = 0, position = 1;
while (n > 0)
{
if (position % 2 == 1)
sum += n % 10;
n = n / 10;
position++;
}
if (sum % k == 0)
return true ;
return false ;
}
static public void Main ()
{
int n = 592452;
int k = 3;
if (SumDivisible(n, k))
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
|
PHP
<?php
function SumDivisible( $n , $k )
{
$sum = 0;
$position = 1;
while ( $n > 0)
{
if ( $position % 2 == 1)
$sum += $n % 10;
$n = (int) $n / 10;
$position ++;
}
if ( $sum % $k == 0)
return true;
return false;
}
$n = 592452;
$k = 3;
if (SumDivisible( $n , $k ))
echo "YES" ;
else
echo "NO" ;
?>
|
Javascript
<script>
function SumDivisible(n, k)
{
let sum = 0, position = 1;
while (n > 0) {
if (position % 2 == 1)
sum += n % 10;
n = Math.floor(n / 10);
position++;
}
if (sum % k == 0)
return true ;
return false ;
}
let n = 592452;
let k = 3;
if (SumDivisible(n, k))
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Time Complexity: O(log10n), since each time the value of n is reduced to n/10.
Auxiliary Space: (1), since no extra space has been taken.
Method #2:Using string() method:
- Convert the integer to a string, then traverse the string and find the sum of all odd indices by storing it in sum.
- If the sum is divisible by k, then return True else False.
Below is the implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool sumDivisible( int n, int k)
{
int sum = 0;
string num = to_string(n);
int i;
for (i = 0; i < num.size(); i++) {
if (i % 2 != 0) {
sum = sum + (num[i] - '0' );
}
}
if (sum % k == 0) {
return true ;
}
else {
return false ;
}
}
int main()
{
int n = 592452;
int k = 3;
if (sumDivisible(n, k)) {
cout << ( "YES" );
}
else {
cout << ( "NO" );
}
return 0;
}
|
Java
import java.io.*;
class GFG
{
static boolean sumDivisible( int n, int k)
{
int sum = 0 ;
String num = Integer.toString(n);
int i;
for (i = 0 ; i < num.length(); i++) {
if (i % 2 != 0 ) {
sum = sum + (num.charAt(i) - '0' );
}
}
if (sum % k == 0 ) {
return true ;
}
else {
return false ;
}
}
public static void main(String[] args)
{
int n = 592452 ;
int k = 3 ;
if (sumDivisible(n, k)) {
System.out.println( "YES" );
}
else {
System.out.println( "NO" );
}
}
}
|
Python3
def sumDivisible(n, k):
sum = 0
num = str (n)
for i in range ( len (num)):
if (i % 2 ! = 0 ):
sum = sum + int (num[i])
if sum % k = = 0 :
return True
return False
n = 592452
k = 3
if sumDivisible(n, k) = = True :
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
static bool sumDivisible( int n, int k)
{
int sum = 0;
string num = n.ToString();
int i;
for (i = 0; i < num.Length; i++) {
if (i % 2 != 0) {
sum = sum + (num[i] - '0' );
}
}
if (sum % k == 0) {
return true ;
}
else {
return false ;
}
}
static public void Main ()
{
int n = 592452;
int k = 3;
if (sumDivisible(n, k)) {
Console.Write( "YES" );
}
else {
Console.Write( "NO" );
}
}
}
|
Javascript
<script>
function sumDivisible(n, k){
var sum = 0
var num = n.toString()
for ( var i=0 ; i < num.length ; i++) {
if (i % 2 != 0) {
sum = sum + Number(num[i])
}
}
if (sum % k == 0){
return true ;
}
else {
return false ;
}
}
var n = 592452
var k = 3
if (sumDivisible(n, k)){
document.write( "YES" );
}
else {
document.write( "NO" );
}
</script>
|
Output:
Yes
Time Complexity: O(d), where d is the number of digits in n.
Auxiliary Space: O(d), where d is the number of digits in n.
Share your thoughts in the comments
Please Login to comment...