Check if a larger number is divisible by 13 or not
Last Updated :
12 Aug, 2025
Given a number s represented as a string, determine whether the integer it represents is divisible by 13 or not.
Examples :
Input: s = "2911285"
Output: true
Explanation: 2911285 / 13 = 223945, which is a whole number with no remainder.
Input: s = "27"
Output: false
Explanation: 27 / 13 ≈ 2.0769..., which is not a whole number (there is a remainder).
[Naive Approach] Modulo Division
If the given number is small, we can easily check whether it is divisible by 13 by computing s % 13 and verifying if the result is 0.
Note: This method only works when the number fits within standard integer data types. For very large numbers (such as those represented as strings with hundreds or thousands of digits), this approach may lead to overflow or be unsupported, so a string-based method is required.
C++
#include <iostream>
using namespace std;
bool divBy13(string &s) {
// Convert the string to an integer
int num = stoi(s);
// Check if the number is divisible by 13
return (num % 13 == 0);
}
int main() {
string s = "2911285";
bool isDivisible = divBy13(s);
if (isDivisible) {
cout << "true" << endl;
} else {
cout << "false" << endl;
}
return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool divBy13(const char* s) {
// Convert the string to an integer
int num = atoi(s);
// Check if the number is divisible by 13
return (num % 13 == 0);
}
int main() {
const char* s = "2911285";
bool isDivisible = divBy13(s);
if (isDivisible) {
printf("true\n");
} else {
printf("false\n");
}
return 0;
}
Java
public class GfG {
static boolean divBy13(String s) {
// Convert the string to an integer
int num = Integer.parseInt(s);
// Check if the number is divisible by 13
return (num % 13 == 0);
}
public static void main(String[] args) {
String s = "2911285";
boolean isDivisible = divBy13(s);
if (isDivisible) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}
Python
def divBy13(s):
# Convert the string to an integer
num = int(s)
# Check if the number is divisible by 13
return num % 13 == 0
if __name__ == "__main__":
s = "2911285"
isDivisible = divBy13(s)
if isDivisible:
print("true")
else:
print("false")
C#
using System;
class GfG {
static bool divBy13(string s) {
// Convert the string to an integer
int num = int.Parse(s);
// Check if the number is divisible by 13
return (num % 13 == 0);
}
static void Main() {
string s = "2911285";
bool isDivisible = divBy13(s);
if (isDivisible) {
Console.WriteLine("true");
} else {
Console.WriteLine("false");
}
}
}
JavaScript
function divBy13(s) {
// Convert the string to an integer
const num = parseInt(s);
// Check if the number is divisible by 13
return (num % 13 === 0);
}
// Driver Code
let s = "2911285";
const isDivisible = divBy13(s);
if (isDivisible) {
console.log("true");
} else {
console.log("false");
}
Time Complexity: O(n), n is length of s
Auxiliary Space: O(1)
A number is divisible by 13 if and only if the alternating sum of its 3-digit blocks, taken from right to left, is divisible by 13.
Step by Step approach -
- Pad the number so its length is a multiple of 3
-> If the number of digits is not a multiple of 3, append zeros to the right so each block has exactly 3 digits.
-> Example: "2911285" → "291128500" (after padding with two zeros). - Split into 3-digit blocks from right to left
-> Example: "291128500" → blocks: 500, 128, 291 (right to left order). - Apply alternating signs starting with
+ on the rightmost block
-> Pattern from right to left: + block , - block, + block, …
-> Example: +500 - 128 + 291. - Sum the results
-> Example: 500 - 128 + 291 = 663. - Check divisibility by 13
-> If the sum is divisible by 13, the original number is divisible by 13.
-> Example: 663 % 13 == 0 → divisible.
C++
#include <iostream>
using namespace std;
bool divBy13(string &s){
int len = s.size();
// Special case: if the number is "0"
if (len == 1 && s[0] == '0') {
return true;
}
// Make the length a multiple of 3 by padding zeros at the end
if (len % 3 == 1) {
s += "00";
len += 2;
} else if (len % 3 == 2) {
s += "0";
len += 1;
}
// store alternating sum of 3-digit blocks and multipiler
int sum = 0;
int p = 1;
// Traverse from right to left in steps of 3 digits
for (int i = len - 1; i >= 0; i--)
{
// Extract 3-digit group (units, tens, hundreds)
int group = 0;
group += s[i--] - '0';
group += (s[i--] - '0') * 10;
group += (s[i] - '0') * 100;
sum = sum + group * p;
// Alternate the sign (+, -, +, -, ...)
p *= -1;
}
// Take absolute value (optional since we're checking divisibility)
sum = abs(sum);
return (sum % 13 == 0);
}
int main() {
string s = "2911285";
if (divBy13(s))
cout << "true" << endl;
else
cout << "false" << endl;
return 0;
}
Java
class GfG {
// Function to check divisibility by 13
static boolean divBy13(String s) {
int len = s.length();
// Special case: if the number is "0"
if (len == 1 && s.charAt(0) == '0') {
return true;
}
// Make the length a multiple of 3 by padding zeros at the end
if (len % 3 == 1) {
s += "00";
len += 2;
} else if (len % 3 == 2) {
s += "0";
len += 1;
}
int sum = 0;
int p = 1;
// Traverse from right to left in steps of 3 digits
for (int i = len - 1; i >= 0; i--) {
int group = 0;
group += s.charAt(i--) - '0';
group += (s.charAt(i--) - '0') * 10;
group += (s.charAt(i) - '0') * 100;
sum += group * p;
p *= -1;
}
sum = Math.abs(sum);
return sum % 13 == 0;
}
public static void main(String[] args) {
String s = "2911285";
System.out.println(divBy13(s));
}
}
Python
def divBy13(s):
length = len(s)
# Special case: if the number is "0"
if length == 1 and s[0] == '0':
return True
# Make the length a multiple of 3 by padding zeros at the end
if length % 3 == 1:
s += "00"
length += 2
elif length % 3 == 2:
s += "0"
length += 1
sum_ = 0
p = 1
# Traverse from right to left in steps of 3 digits
i = length - 1
while i >= 0:
group = 0
group += int(s[i])
i -= 1
group += int(s[i]) * 10
i -= 1
group += int(s[i]) * 100
i -= 1
sum_ += group * p
p *= -1
sum_ = abs(sum_)
return sum_ % 13 == 0
if __name__ == "__main__":
s = "2911285"
isDivisible = divBy13(s)
if isDivisible:
print("true")
else:
print("false")
C#
using System;
class GfG {
static bool divBy13(string s) {
int len = s.Length;
// Special case: if the number is "0"
if (len == 1 && s[0] == '0') {
return true;
}
// Make the length a multiple of 3 by
// padding zeros at the end
if (len % 3 == 1) {
s += "00";
len += 2;
} else if (len % 3 == 2) {
s += "0";
len += 1;
}
int sum = 0;
int p = 1;
// Traverse from right to left in steps of 3 digits
for (int i = len - 1; i >= 0; i--) {
int group = 0;
group += s[i--] - '0';
group += (s[i--] - '0') * 10;
group += (s[i] - '0') * 100;
sum += group * p;
p *= -1;
}
sum = Math.Abs(sum);
return (sum % 13 == 0);
}
static void Main() {
string s = "2911285";
bool isDivisible = divBy13(s);
if (isDivisible) {
Console.WriteLine("true");
} else {
Console.WriteLine("false");
}
}
}
JavaScript
function divBy13(s) {
let len = s.length;
// Special case: if the number is "0"
if (len === 1 && s[0] === '0') {
return true;
}
// Make the length a multiple of 3 by padding zeros at the end
if (len % 3 === 1) {
s += "00";
len += 2;
} else if (len % 3 === 2) {
s += "0";
len += 1;
}
let sum = 0;
let p = 1;
// Traverse from right to left in steps of 3 digits
for (let i = len - 1; i >= 0; i--) {
let group = 0;
group += parseInt(s[i--]);
group += parseInt(s[i--]) * 10;
group += parseInt(s[i]) * 100;
sum += group * p;
p *= -1;
}
sum = Math.abs(sum);
return sum % 13 === 0;
}
// Driver Code
const s = "2911285";
console.log(divBy13(s));
Time Complexity: O(n), n is length of s
Auxiliary Space: O(1)
[Expected Approach 2] String-Based Modulo
We process the number digit by digit from left to right, maintaining the remainder modulo 13 at each step using the formula:
rem = (rem * 10 + digit) % 13.
Step by Step Approach -
- Initialize remainder:
-> rem = 0 - Process each digit from left to right:
-> Digit '2': rem = (0 * 10 + 2) % 13 = 2
-> Digit '9': rem = (2 * 10 + 9) % 13 = 29 % 13 = 3
-> Digit '1': rem = (3 * 10 + 1) % 13 = 31 % 13 = 5
-> Digit '1': rem = (5 * 10 + 1) % 13 = 51 % 13 = 12
-> Digit '2': rem = (12 * 10 + 2) % 13 = 122 % 13 = 5
-> Digit '8': rem = (5 * 10 + 8) % 13 = 58 % 13 = 6
-> Digit '5': rem = (6 * 10 + 5) % 13 = 65 % 13 = 0 - Since final rem = 0, the number 2911285 is divisible by 13.
C++
#include <iostream>
using namespace std;
bool divBy13(string& s) {
// Stores running remainder
int rem = 0;
// Process each digit and compute remainder modulo 13
for (char ch : s) {
rem = (rem * 10 + (ch - '0')) % 13;
}
// Final check for divisibility
return rem == 0;
}
int main() {
string s = "2911285";
if (divBy13(s))
cout << "true" << endl;
else
cout << "false" << endl;
return 0;
}
Java
class GfG {
static boolean divBy13(String s) {
// Stores running remainder
int rem = 0;
// Process each digit and compute remainder modulo 13
for (int i = 0; i < s.length(); i++) {
rem = (rem * 10 + (s.charAt(i) - '0')) % 13;
}
// Final check: if remainder is 0, number is divisible by 13
return rem == 0;
}
public static void main(String[] args) {
String s = "2911285";
if (divBy13(s))
System.out.println("true");
else
System.out.println("false");
}
}
Python
def divBy13(s):
# Stores running remainder
rem = 0
# Process each digit and compute remainder modulo 13
for ch in s:
rem = (rem * 10 + int(ch)) % 13
# Final check for divisibility
return rem == 0
if __name__ == "__main__":
s = "2911285"
if divBy13(s):
print("true")
else:
print("false")
C#
using System;
class GfG {
static bool divBy13(string s) {
// Stores running remainder
int rem = 0;
// Process each digit and compute remainder modulo 13
foreach (char ch in s) {
rem = (rem * 10 + (ch - '0')) % 13;
}
// Check if the final remainder is zero
return rem == 0;
}
static void Main() {
string s = "2911285";
if (divBy13(s))
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
JavaScript
function divBy13(s) {
// Stores running remainder
let rem = 0;
// Process each digit and compute remainder modulo 13
for (let i = 0; i < s.length; i++) {
rem = (rem * 10 + (s.charCodeAt(i) - '0'.charCodeAt(0))) % 13;
}
// Final check for divisibility
return rem === 0;
}
// Driver Code
let s = "2911285";
if (divBy13(s))
console.log("true");
else
console.log("false");
Time Complexity: O(n), n is length of s
Auxiliary Space: O(1)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem