Check if Binary Divisible by 10

Last Updated : 24 Jun, 2026

Given a binary string s, return true if its decimal representation is divisible by 10, otherwise return false.

Examples:

Input: s = "1010"
Output: true
Explanation: The decimal value of "1010" is 10, which is divisible by 10.

Input: s = "10"
Output: false
Explanation: The decimal value of "10" is 2, which is not divisible by 10.

Try It Yourself
redirect icon

[Approach 1] Convert Binary to Decimal - O(|s|) Time and O(1) Space

The idea is to convert the binary string into its decimal value and then check whether the decimal value is divisible by 10. This works only for small binary strings because the decimal value can become too large for integer data types.

C++
#include <iostream>
#include <string>
using namespace std;

bool isDivisible(string& s) {
    int num = 0;

    // Convert binary string to decimal.
    for (char ch : s) {
        num = num * 2 + (ch - '0');
    }

    return num % 10 == 0;
}

int main() {
    string s = "1010";
    cout << (isDivisible(s) ? "true" : "false") << endl;

    s = "10";
    cout << (isDivisible(s) ? "true" : "false") << endl;

    return 0;
}
Java
class GFG {
    static boolean isDivisible(String s) {
        int num = 0;

        // Convert binary string to decimal.
        for (int i = 0; i < s.length(); i++) {
            num = num * 2 + (s.charAt(i) - '0');
        }

        return num % 10 == 0;
    }

    public static void main(String[] args) {
        System.out.println(isDivisible("1010") ? "true" : "false");
        System.out.println(isDivisible("10") ? "true" : "false");
    }
}
Python
def isDivisible(s):
    num = 0

    # Convert binary string to decimal.
    for ch in s:
        num = num * 2 + int(ch)

    return num % 10 == 0


if __name__ == "__main__":
    print(str(isDivisible("1010")).lower())
    print(str(isDivisible("10")).lower())
C#
using System;

class GFG {
    static bool isDivisible(string s) {
        int num = 0;

        // Convert binary string to decimal.
        foreach (char ch in s) {
            num = num * 2 + (ch - '0');
        }

        return num % 10 == 0;
    }

    static void Main() {
        Console.WriteLine(isDivisible("1010").ToString().ToLower());
        Console.WriteLine(isDivisible("10").ToString().ToLower());
    }
}
JavaScript
function isDivisible(s) {
    let num = 0;

    // Convert binary string to decimal.
    for (const ch of s) {
        num = num * 2 + Number(ch);
    }

    return num % 10 === 0;
}

// Driver Code
console.log(isDivisible("1010") ? "true" : "false");
console.log(isDivisible("10") ? "true" : "false");

Output
true
false

[Approach 2] Using Powers of 2 Modulo 10 - O(|s|) Time and O(1) Space

The idea is to avoid converting the complete binary string into decimal. From right to left, powers of 2 modulo 10 repeat in the cycle 2, 4, 8, 6. We add only those values where the binary digit is 1 and finally check whether the sum is divisible by 10.

C++
#include <iostream>
#include <string>
using namespace std;

bool isDivisible(string& s) {
    int n = s.length();

    if (s[n - 1] == '1') return false;

    int sum = 0;

    // Add contribution of each set bit modulo 10.
    for (int i = n - 2; i >= 0; i--) {
        if (s[i] == '1') {
            int pos = n - i - 1;

            if (pos % 4 == 1) sum += 2;
            else if (pos % 4 == 2) sum += 4;
            else if (pos % 4 == 3) sum += 8;
            else sum += 6;

            sum %= 10;
        }
    }

    return sum % 10 == 0;
}

int main() {
    string s = "1010";
    cout << (isDivisible(s) ? "true" : "false") << endl;

    s = "10";
    cout << (isDivisible(s) ? "true" : "false") << endl;

    return 0;
}
Java
class GFG {
    static boolean isDivisible(String s) {
        int n = s.length();

        if (s.charAt(n - 1) == '1') return false;

        int sum = 0;

        // Add contribution of each set bit modulo 10.
        for (int i = n - 2; i >= 0; i--) {
            if (s.charAt(i) == '1') {
                int pos = n - i - 1;

                if (pos % 4 == 1) sum += 2;
                else if (pos % 4 == 2) sum += 4;
                else if (pos % 4 == 3) sum += 8;
                else sum += 6;

                sum %= 10;
            }
        }

        return sum % 10 == 0;
    }

    public static void main(String[] args) {
        System.out.println(isDivisible("1010") ? "true" : "false");
        System.out.println(isDivisible("10") ? "true" : "false");
    }
}
Python
def isDivisible(s):
    n = len(s)

    if s[-1] == '1':
        return False

    total = 0

    # Add contribution of each set bit modulo 10.
    for i in range(n - 2, -1, -1):
        if s[i] == '1':
            pos = n - i - 1

            if pos % 4 == 1:
                total += 2
            elif pos % 4 == 2:
                total += 4
            elif pos % 4 == 3:
                total += 8
            else:
                total += 6

            total %= 10

    return total % 10 == 0


if __name__ == "__main__":
    print(str(isDivisible("1010")).lower())
    print(str(isDivisible("10")).lower())
C#
using System;

class GFG {
    static bool isDivisible(string s) {
        int n = s.Length;

        if (s[n - 1] == '1') return false;

        int sum = 0;

        // Add contribution of each set bit modulo 10.
        for (int i = n - 2; i >= 0; i--) {
            if (s[i] == '1') {
                int pos = n - i - 1;

                if (pos % 4 == 1) sum += 2;
                else if (pos % 4 == 2) sum += 4;
                else if (pos % 4 == 3) sum += 8;
                else sum += 6;

                sum %= 10;
            }
        }

        return sum % 10 == 0;
    }

    static void Main() {
        Console.WriteLine(isDivisible("1010").ToString().ToLower());
        Console.WriteLine(isDivisible("10").ToString().ToLower());
    }
}
JavaScript
function isDivisible(s) {
    const n = s.length;

    if (s[n - 1] === '1') return false;

    let sum = 0;

    // Add contribution of each set bit modulo 10.
    for (let i = n - 2; i >= 0; i--) {
        if (s[i] === '1') {
            const pos = n - i - 1;

            if (pos % 4 === 1) sum += 2;
            else if (pos % 4 === 2) sum += 4;
            else if (pos % 4 === 3) sum += 8;
            else sum += 6;

            sum %= 10;
        }
    }

    return sum % 10 === 0;
}

// Driver Code
console.log(isDivisible("1010") ? "true" : "false");
console.log(isDivisible("10") ? "true" : "false");

Output
true
false

[Approach 3] Remainder Modulo 10 - O(|s|) Time and O(1) Space

The idea is to process the binary string from left to right and keep only the remainder modulo 10. For every bit, the current value becomes previousValue * 2 + bit. Since we only need divisibility by 10, we store (remainder * 2 + bit) % 10 instead of the full number.

Let us understand with example:

For s = "1010":

  • rem = 0
  • read '1' -> rem = (0 * 2 + 1) % 10 = 1
  • read '0' -> rem = (1 * 2 + 0) % 10 = 2
  • read '1' -> rem = (2 * 2 + 1) % 10 = 5
  • read '0' -> rem = (5 * 2 + 0) % 10 = 0

Final remainder is 0, so the binary number is divisible by 10.

C++
#include <iostream>
#include <string>
using namespace std;

bool isDivisible(string& s) {
    int rem = 0;

    // Keep only remainder modulo 10.
    for (char ch : s) {
        rem = (rem * 2 + (ch - '0')) % 10;
    }

    return rem == 0;
}

int main() {
    string s = "1010";
    cout << (isDivisible(s) ? "true" : "false") << endl;

    s = "10";
    cout << (isDivisible(s) ? "true" : "false") << endl;

    return 0;
}
Java
class GFG {
    static boolean isDivisible(String s) {
        int rem = 0;

        // Keep only remainder modulo 10.
        for (int i = 0; i < s.length(); i++) {
            rem = (rem * 2 + (s.charAt(i) - '0')) % 10;
        }

        return rem == 0;
    }

    public static void main(String[] args) {
        System.out.println(isDivisible("1010") ? "true" : "false");
        System.out.println(isDivisible("10") ? "true" : "false");
    }
}
Python
def isDivisible(s):
    rem = 0

    # Keep only remainder modulo 10.
    for ch in s:
        rem = (rem * 2 + int(ch)) % 10

    return rem == 0


if __name__ == "__main__":
    print(str(isDivisible("1010")).lower())
    print(str(isDivisible("10")).lower())
C#
using System;

class GFG {
    static bool isDivisible(string s) {
        int rem = 0;

        // Keep only remainder modulo 10.
        foreach (char ch in s) {
            rem = (rem * 2 + (ch - '0')) % 10;
        }

        return rem == 0;
    }

    static void Main() {
        Console.WriteLine(isDivisible("1010").ToString().ToLower());
        Console.WriteLine(isDivisible("10").ToString().ToLower());
    }
}
JavaScript
function isDivisible(s) {
    let rem = 0;

    // Keep only remainder modulo 10.
    for (const ch of s) {
        rem = (rem * 2 + Number(ch)) % 10;
    }

    return rem === 0;
}

// Driver Code
console.log(isDivisible("1010") ? "true" : "false");
console.log(isDivisible("10") ? "true" : "false");

Output
true
false
Comment