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++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// function that checks the
// divisibility of the sum
// of the digits at odd places
// of the given number
bool SumDivisible(int n, int k)
{
int sum = 0, position = 1;
while (n > 0) {
// if position is odd
if (position % 2 == 1)
sum += n % 10;
n = n / 10;
position++;
}
if (sum % k == 0)
return true;
return false;
}
// Driver code
int main()
{
int n = 592452;
int k = 3;
if (SumDivisible(n, k))
cout << "YES";
else
cout << "NO";
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class solution
{
// function that checks the
// divisibility of the sum
// of the digits at odd places
// of the given number
static boolean SumDivisible(int n, int k)
{
int sum = 0, position = 1;
while (n > 0) {
// if position is odd
if (position % 2 == 1)
sum += n % 10;
n = n / 10;
position++;
}
if (sum % k == 0)
return true;
return false;
}
// Driver code
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");
}
}
//This code is contributed by Surendra_Gangwar
Python 3
# Python 3 implementation of the approach
# function that checks the divisibility
# of the sum of the digits at odd places
# of the given number
def SumDivisible(n, k):
sum = 0
position = 1
while (n > 0) :
# if position is odd
if (position % 2 == 1):
sum += n % 10
n = n // 10
position += 1
if (sum % k == 0):
return True
return False
# Driver code
if __name__ =="__main__":
n = 592452
k = 3
if (SumDivisible(n, k)):
print("YES")
else:
print("NO")
# This code is contributed
# by ChitraNayal
C#
// C# implementation of the approach
using System;
class GFG
{
// function that checks the
// divisibility of the sum
// of the digits at odd places
// of the given number
static bool SumDivisible(int n, int k)
{
int sum = 0, position = 1;
while (n > 0)
{
// if position is odd
if (position % 2 == 1)
sum += n % 10;
n = n / 10;
position++;
}
if (sum % k == 0)
return true;
return false;
}
// Driver code
static public void Main ()
{
int n = 592452;
int k = 3;
if (SumDivisible(n, k))
Console.WriteLine("YES");
else
Console.WriteLine("NO");
}
}
// This code is contributed by Sachin
PHP
<?php
// PHP implementation of the approach
// function that checks the divisibility
// of the sum of the digits at odd places
// of the given number
function SumDivisible($n, $k)
{
$sum = 0;
$position = 1;
while ($n > 0)
{
// if position is odd
if ($position % 2 == 1)
$sum += $n % 10;
$n = (int)$n / 10;
$position++;
}
if ($sum % $k == 0)
return true;
return false;
}
// Driver code
$n = 592452;
$k = 3;
if (SumDivisible($n, $k))
echo "YES";
else
echo "NO";
// This code is contributed
// by Sach_Code
?>
JavaScript
<script>
// JavaScript implementation of the approach
// function that checks the
// divisibility of the sum
// of the digits at odd places
// of the given number
function SumDivisible(n, k)
{
let sum = 0, position = 1;
while (n > 0) {
// if position is odd
if (position % 2 == 1)
sum += n % 10;
n = Math.floor(n / 10);
position++;
}
if (sum % k == 0)
return true;
return false;
}
// Driver code
let n = 592452;
let k = 3;
if (SumDivisible(n, k))
document.write("YES");
else
document.write("NO");
// This code is contributed by Surbhi Tyagi.
</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++
// C++ implementation of the
// above approach
#include <bits/stdc++.h>
using namespace std;
bool sumDivisible(int n, int k)
{
int sum = 0;
// Converting integer to string
string num = to_string(n);
int i;
// Traversing the string
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;
}
}
// Driver code
int main()
{
int n = 592452;
int k = 3;
if (sumDivisible(n, k)) {
cout << ("YES");
}
else {
cout << ("NO");
}
return 0;
}
// This code is contributed by gauravrajput1
Java
// Java implementation of the
// above approach
import java.io.*;
class GFG
{
static boolean sumDivisible(int n, int k)
{
int sum = 0;
// Converting integer to string
String num = Integer.toString(n);
int i;
// Traversing the string
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;
}
}
// Driver code
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");
}
}
}
// This code is contributed by shivanisinghss2110
Python3
# Python3 implementation of the
# above approach
def sumDivisible(n, k):
sum = 0
# Converting integer to string
num = str(n)
# Traversing the string
for i in range(len(num)):
if(i % 2 != 0):
sum = sum+int(num[i])
if sum % k == 0:
return True
return False
# Driver code
n = 592452
k = 3
if sumDivisible(n, k) == True:
print("YES")
else:
print("NO")
# This code is contributed by vikkycirus
C#
// C# implementation of the
// above approach
using System;
class GFG
{
static bool sumDivisible(int n, int k)
{
int sum = 0;
// Converting integer to string
string num = n.ToString();
int i;
// Traversing the string
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;
}
}
// Driver code
static public void Main ()
{
int n = 592452;
int k = 3;
if (sumDivisible(n, k)) {
Console.Write("YES");
}
else {
Console.Write("NO");
}
}
}
// This code is contributed by shivanisinghss2110
JavaScript
<script>
// javascript implementation of the
// above approach
function sumDivisible(n, k){
var sum = 0
// Converting integer to string
var num = n.toString()
// Traversing the string
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;
}
}
// Driver code
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.
Similar Reads
Check whether product of digits at even places is divisible by sum of digits at odd place of a number 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: TRUESince, 1 * 7 = 7, which is divisibl
13 min read
Check if the sum of digits of number is divisible by all of its digits Given an integer N, the task is to check whether the sum of digits of the given number is divisible by all of its digits or not. If divisible then print Yes else print No. Examples: Input: N = 12 Output: No Sum of digits = 1 + 2 = 3 3 is divisible by 1 but not 2. Input: N = 123 Output: Yes Approach:
14 min read
Program to check if a number is divisible by sum of its digits Given an integer N, the task is to check whether the number is divisible by the sum of its digits or not. If divisible, then print âYESâ else print âNOâ. Examples: Input: N = 12 Output: YES Explanation: As sum of digits of 12 = 1 + 2 = 3 and 12 is divisible by 3 So the output is YES Input: N = 123 O
7 min read
Find the sum of digits of a number at even and odd places Given a number N, the task is to find the sum of digits of a number at even and odd places. Examples: Input: N = 54873 Output: Sum odd = 16 Sum even = 11 Input: N = 457892 Output: Sum odd = 20 Sum even = 15 Approach: First, calculate the reverse of the given number.To the reverse number we apply mod
13 min read
Check if the sum of digits of a number N divides it Given a number n, the task is to check if the sum of digits of the given number divides the number or not. Examples: Input : n = 12Output : YesExplanation: Sum of digits = 1+2 =3 and 3 divides 12.Input : n = 15Output : NoExplanation: Sum of digits = 1+5 =6 and 15 % 6 != 0.Using Iterative Method - O(
6 min read
Program to check if a number is divisible by any of its digits Given an integer N where 1 \leq n \leq 10^{18} . The task is to check whether the number is not divisible by any of its digit. If the given number N is divisible by any of its digits then print "YES" else print "NO". Examples: Input : N = 5115Output : YESExplanation: 5115 is divisible by both 1 and
10 min read