Check if a large number is divisible by 11 or not
Last Updated :
19 Jun, 2025
Given a number in the form of string s, Check if the number is divisible by 11 or not. The input number may be large and it may not be possible to store it even if we use long long int.
Examples:
Input: s = "76945"
Output: true
Explanation: s when divided by 11 gives 0 as remainder.
Input: s = "7695"
Output: false
Explanation: s does not give 0 as remainder when divided by 11.
Input: s = "1234567589333892"
Output: true
Explanation: s when divided by 11 gives 0 as remainder.
[Naive Approach]: Modulo Division Method
Checking given number is divisible by 11 or not by using the modulo division operator "%".
Note: This approach may not work in some programming languages for very large input strings, due to limitations such as integer overflow or memory constraints..
C++
#include <iostream>
#include <string>
using namespace std;
bool divBy11(string &s) {
// Convert string to int
int n = stoi(s);
return n % 11 == 0;
}
int main() {
string s = "76945";
if (divBy11(s))
cout << "true" << endl;
else
cout << "false" << endl;
return 0;
}
Java
class GfG {
public static boolean divBy11(String s) {
// Convert string to int
int n = Integer.parseInt(s);
return n % 11 == 0;
}
public static void main(String[] args) {
String s = "76945";
if (divBy11(s))
System.out.println("true");
else
System.out.println("false");
}
}
Python
def divBy11(s):
# Convert string to integer
n = int(s)
return n % 11 == 0
if __name__ == "__main__":
s = "76945"
if divBy11(s):
print("true")
else:
print("false")
C#
using System;
class GfG
{
static bool divBy11(string s)
{
// Convert string to int
int n = int.Parse(s);
return n % 11 == 0;
}
static void Main()
{
string s = "76945";
if (divBy11(s))
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
JavaScript
function divBy11(s) {
// Convert string to int
let n = parseInt(s);
return n % 11 === 0;
}
// Driver Code
const s = "76945";
if (divBy11(s)) {
console.log("true");
} else {
console.log("false");
}
Time Complexity: O(n), where n is length of s.
Auxiliary Space: O(1)
Since input number may be very large, we cannot use n % 11 to check if a number is divisible by 11 or not. The idea is based on following fact.
A number is divisible by 11 if difference of following two is divisible by 11.
- Sum of digits at odd places.
- Sum of digits at even places.
Illustration:
For example, let us consider 76945
Sum of digits at odd places : 7 + 9 + 5, Sum of digits at even places : 6 + 4
Difference of two sums = 21 - 10 = 11. Since difference is divisible by 11, the number 76945 is divisible by 11.
How does this work?
Let us consider 7694, we can write it as: 7694 = 7*1000 + 6*100 + 9*10 + 4
The proof is based on below observation:
- Remainder of 10i divided by 11 = 1 if i is even
- Remainder of 10i divided by 11 = 10 ≡ -1 if i is odd
So the powers of 10 only result in values either 1 or -1. Remainder of "7*1000 + 6*100 + 9*10 + 4" divided by 11 can be written as :
7*(-1) + 6*1 + 9*(-1) + 4*1
The above expression is basically difference between sum of even digits and odd digits.
C++
#include <iostream>
#include <string>
using namespace std;
int divBy11(string &s){
int n = s.length();
int oddDigSum = 0, evenDigSum = 0;
for (int i = 0; i < n; i++){
// check for even index
if (i % 2 == 0)
oddDigSum += (s[i] - '0');
else
evenDigSum += (s[i] - '0');
}
// Check divisibility by 11
// using digit sum difference
return ((oddDigSum - evenDigSum) % 11 == 0);
}
int main(){
string s = "76945";
cout << (divBy11(s) ? "true" : "false");
return 0;
}
Java
class GfG {
public static boolean divBy11(String s) {
int n = s.length();
int oddDigSum = 0, evenDigSum = 0;
for (int i = 0; i < n; i++) {
int digit = s.charAt(i) - '0';
// check for even index
if (i % 2 == 0)
oddDigSum += digit;
else
evenDigSum += digit;
}
// Check divisibility by 11
// using digit sum difference
return (oddDigSum - evenDigSum) % 11 == 0;
}
public static void main(String[] args) {
String s = "76945";
System.out.println(divBy11(s) ? "true" : "false");
}
}
Python
def divBy11(s):
odd_dig_sum = 0
even_dig_sum = 0
for i in range(len(s)):
digit = int(s[i])
# check for even index
if i % 2 == 0:
odd_dig_sum += digit
else:
even_dig_sum += digit
# Check divisibility by 11
# using digit sum difference
return (odd_dig_sum - even_dig_sum) % 11 == 0
if __name__ == "__main__":
s = "76945"
print("true" if divBy11(s) else "false")
C#
using System;
class GfG{
static bool divBy11(string s){
int oddDigSum = 0, evenDigSum = 0;
for (int i = 0; i < s.Length; i++){
int digit = s[i] - '0';
// check for even index
if (i % 2 == 0)
oddDigSum += digit;
else
evenDigSum += digit;
}
// Check divisibility by 11
// using digit sum difference
return (oddDigSum - evenDigSum) % 11 == 0;
}
static void Main(string[] args){
string s = "76945";
Console.WriteLine(divBy11(s) ? "true" : "false");
}
}
JavaScript
function divBy11(s) {
let oddDigSum = 0, evenDigSum = 0;
for (let i = 0; i < s.length; i++) {
let digit = parseInt(s[i]);
// check for even index
if (i % 2 === 0)
oddDigSum += digit;
else
evenDigSum += digit;
}
// Check divisibility by 11
// using digit sum difference
return (oddDigSum - evenDigSum) % 11 === 0;
}
// Driver Code
const s = "76945";
console.log(divBy11(s) ? "true" : "false");
Time Complexity: O(n), where n is length of s.
Auxiliary Space: O(1)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem