Divisibility by 8

Last Updated : 21 Apr, 2026


Given a string s representing a non-negative decimal number, determine whether the number is divisible by 8. Return true if it is divisible by 8, otherwise return false.

Examples:  

Input: s = "16"
Output: true
Explanation: The given number is divisible by 8. So the answer is true.

Input: s = "54141111648421214584416464555"
Output: false
Explanation: Given Number is not divisible by 8. So the answer for this is false.

Try It Yourself
redirect icon

Using Mathematical Divisibility Rule - O(1) Time and O(1) Space

A number is divisible by 8 if its last three digits are divisible by 8. So, check only the last three digits when the string length is greater than 3; otherwise, check the whole number. 

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

bool isDivBy8(string &s) {
    int len = s.length();
    int number = 0;

    // Only last 3 digits matter for divisibility by 8
    int start = (len > 3)? len - 3 : 0;

    // Convert required part of string into number
    for (int i = start; i < len; i++) {
        number = number * 10 + (s[i] - '0');
    }

    // Check divisibility by 8
    return (number % 8 == 0);
}

// Driver Code
int main() {
    string s = "123456";
    cout << isDivBy8(s);
    return 0;
}
Java
import java.util.*;

class GFG {

    static boolean isDivBy8(String s) {
        int len = s.length();
        int number = 0;

        // Only last 3 digits matter for divisibility by 8
        int start = (len > 3) ? len - 3 : 0;

        // Convert required part of string into number
        for (int i = start; i < len; i++) {
            number = number * 10 + (s.charAt(i) - '0');
        }

        // Check divisibility by 8
        return (number % 8 == 0);
    }

    // Driver Code
    public static void main(String[] args) {
        String s = "123456";
        System.out.println(isDivBy8(s));
    }
}
Python
def isDivBy8(s):
    len_s = len(s)
    number = 0

    # Only last 3 digits matter for divisibility by 8
    start = len_s - 3 if len_s > 3 else 0

    # Convert required part of string into number
    for i in range(start, len_s):
        number = number * 10 + (ord(s[i]) - ord('0'))

    # Check divisibility by 8
    return number % 8 == 0

# Driver Code
if __name__ == "__main__":
    s = "123456"
    print(isDivBy8(s))
C#
using System;

class GFG {

    static bool isDivBy8(string s) {
        int len = s.Length;
        int number = 0;

        // Only last 3 digits matter for divisibility by 8
        int start = (len > 3) ? len - 3 : 0;

        // Convert required part of string into number
        for (int i = start; i < len; i++) {
            number = number * 10 + (s[i] - '0');
        }

        // Check divisibility by 8
        return (number % 8 == 0);
    }

    // Driver Code
    static void Main() {
        string s = "123456";
        Console.WriteLine(isDivBy8(s));
    }
}
JavaScript
function isDivBy8(s) {
    let len = s.length;
    let number = 0;

    // Only last 3 digits matter for divisibility by 8
    let start = (len > 3)? len - 3 : 0;

    // Convert required part of string into number
    for (let i = start; i < len; i++) {
        number = number * 10 + (s.charCodeAt(i) - '0'.charCodeAt(0));
    }

    // Check divisibility by 8
    return (number % 8 === 0);
}

// Driver Code
let s = "123456";
console.log(isDivBy8(s));
PHP
<?php
function isDivBy8($s) {
    $len = strlen($s);
    $number = 0;

    // Only last 3 digits matter for divisibility by 8
    $start = ($len > 3)? $len - 3 : 0;

    // Convert required part of string into number
    for ($i = $start; $i < $len; $i++) {
        $number = $number * 10 + ($s[$i] - '0');
    }

    // Check divisibility by 8
    return ($number % 8 == 0);
}

// Driver Code
$s = "123456";
echo isDivBy8($s);
?>

Output
1
Comment