Check if a large number is divisible by 8 or not

23 May, 2022
Given a number, the task is to check if a number is divisible by 8 or not. The input number may be large and it may not be possible to store even if we use long long int.Examples:  
Input  : n = 1128
Output : Yes

Input : n = 1124
Output : No

Input  : n = 363588395960667043875487
Output : No
 
Since input number may be very large, we cannot use n % 8 to check if a number is divisible by 8 or not, especially in languages like C/C++. The idea is based on following fact. 
A number is divisible by 8 if number formed by last three digits of it is divisible by 8.
Illustration:  
For example, let us consider 76952 
Number formed by last three digits = 952
Since 952 is divisible by 8, answer is YES.
How does this work? 
Let us consider 76952, we can write it as
76942 = 7*10000 + 6*1000 + 9*100 + 5*10 + 2

The proof is based on below observation:
Remainder of 10i divided by 8 is 0 if i greater 
than or equal to three. Note than 10000, 
1000,... etc lead to remainder 0 when divided by 8.

So remainder of "7*10000 + 6*1000 + 9*100 + 
5*10 + 2" divided by 8 is equivalent to remainder 
of following : 
0 + 0 + 9*100 + 5*10 + 2 = 52
Therefore we can say that the whole number is 
divisible by 8 if 952 is divisible by 8.
Below is implementation of above fact : 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find if a number is divisible by// 8 or not#include<bits/stdc++.h>using namespace std; // Function to find that number divisible by// 8 or notbool check(string str){    int n = str.length();     // Empty string    if (n == 0)        return false;     // If there is single digit    if (n == 1)        return ((str[0]-'0')%8 == 0);     // If there is double digit    if (n == 2)        return (((str[n-2]-'0')*10 + (str[n-1]-'0'))%8 == 0);     // If number formed by last three digits is    // divisible by 8.    int last = str[n-1] - '0';    int second_last = str[n-2] - '0';    int third_last = str[n-3] - '0';     return ((third_last*100 + second_last*10 + last) % 8 == 0);} // Driver codeint main(){    string str = "76952";    check(str)?  cout << "Yes" : cout << "No ";    return 0;}
// Java program to find if a number is// divisible by 8 or notclass IsDivisible{    // Function to find that number divisible by    // 8 or not    static boolean check(String str)    {        int n = str.length();              // Empty string        if (n == 0)            return false;              // If there is single digit        if (n == 1)            return ((str.charAt(0)-'0')%8 == 0);              // If there is double digit        if (n == 2)            return (((str.charAt(n-2)-'0')*10 + (str.charAt(n-1)-'0'))%8 == 0);              // If number formed by last three digits is        // divisible by 8.        int last = str.charAt(n-1) - '0';        int second_last = str.charAt(n-2) - '0';        int third_last = str.charAt(n-3) - '0';              return ((third_last*100 + second_last*10 + last) % 8 == 0);    }         // main function    public static void main (String[] args)    {        String str = "76952";        if(check(str))            System.out.println("Yes");        else            System.out.println("No");    }}
# Python 3 program to find# if a number is divisible# by 8 or not  # Function to find that# number divisible by 8# or notdef check(st) :    n = len(st)         # Empty string    if (n == 0) :        return False     # If there is single digit    if (n == 1) :        return ((int)(st[0]) % 8 == 0)     # If there is double digit    if (n == 2) :        return ((int)(st[n - 2]) * 10 +          ((int)(str[n - 1]) % 8 == 0))     # If number formed by last    # three digits is divisible    # by 8.    last = (int)(st[n - 1])    second_last = (int)(st[n - 2])    third_last = (int)(st[n - 3])     return ((third_last*100 + second_last*10 +                               last) % 8 == 0)  # Driver codest = "76952" if(check(st)) :    print("Yes")else :    print("No ")     # This code is contributed by Nikita tiwari
// C# program to find if a number// is divisible by 8 or notusing System; class IsDivisible{    // Function to find that number    // divisible by 8 or not    static bool check(String str)    {        int n = str.Length;             // Empty string        if (n == 0)            return false;             // If there is single digit        if (n == 1)            return ((str[0] - '0') %8  == 0);             // If there is double digit        if (n == 2)            return (((str[n - 2] - '0') * 10 +            (str[n - 1] - '0')) % 8 == 0);             // If number formed by last three        // digits is divisible by 8        int last = str[n - 1] - '0';        int second_last = str[n - 2] - '0';        int third_last = str[n - 3] - '0';             return ((third_last * 100 + second_last                         * 10 + last) % 8 == 0);    }         // Driver Code    public static void Main ()    {        String str = "76952";        if(check(str))            Console.Write("Yes");        else            Console.Write("No");    }} // This Code is contributed by Nitin Mittal.
<?php// PHP program to find if a number// is divisible by 8 or not // Function to find that number// divisible by 8 or notfunction check($str){    $n = strlen($str);     // Empty string    if ($n == 0)        return false;     // If there is single digit    if ($n == 1)        return (($str[0] - '0') %                          8 == 0);     // If there is double digit    if ($n == 2)        return ((($str[$n - 2] - '0') *               10 + ($str[$n - 1] - '0'))                                % 8 == 0);     // If number formed by last three    // digits is divisible by 8.    $last = $str[$n - 1] - '0';    $second_last = $str[$n - 2] - '0';    $third_last = $str[$n - 3] - '0';     return (($third_last * 100 +             $second_last * 10 +             $last) % 8 == 0);} // Driver code$str = "76952";$x = check($str)? "Yes" : "No ";echo($x); // This code is contributed by Ajit.?>
<script> // JavaScript program for the above approach     // Function to find that number     // divisible by 8 or not    function check(str)    {        let n = str.length;               // Empty string        if (n == 0)            return false;               // If there is single digit        if (n == 1)            return ((str[0] - '0') %8  == 0);               // If there is double digit        if (n == 2)            return (((str[n - 2] - '0') * 10 +             (str[n - 1] - '0')) % 8 == 0);               // If number formed by last three         // digits is divisible by 8        let last = str[n - 1] - '0';        let second_last = str[n - 2] - '0';        let third_last = str[n - 3] - '0';               return ((third_last * 100 + second_last                          * 10 + last) % 8 == 0);    } // Driver Code          let str = "76952";     if(check(str))        document.write("Yes");     else         document.write("No"); // This code is contributed by splevel62.</script>
Output:
Yes
Time Complexity: O(1), as we are not using any loops for traversing.
Auxiliary Space: O(1), as we are not using any extra space.This article is contributed by DANISH_RAZA . If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
nitin mittal
jit_t
splevel62
rohitsingh07052
divisibility
number-digits
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Prime Numbers
Sieve of Eratosthenes
Program to find GCD or HCF of two numbers
Find minimum number of coins that make a given value
Minimum number of jumps to reach end
Algorithm to solve Rubik's Cube
The Knight's tour problem | Backtracking-1
Program for Decimal to Binary Conversion