Check if a large number is divisible by 11 or not - GeeksforGeeks

23 Mar, 2021
Given a number, the task is to 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 : n = 76945
Output : Yes

Input  : n = 1234567589333892
Output : Yes

Input  : n = 363588395960667043875487
Output : No
 
Since input number may be very large, we cannot use n % 11 to check if a number is divisible by 11 or not, especially in languages like C/C++. 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.
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 7945 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 is 1 if i is even
Remainder of 10i divided by 11 is -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.
Below is implementation of above fact : 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find if a number is divisible by// 11 or not#include<bits/stdc++.h>using namespace std; // Function to find that number divisible by 11 or notint check(string str){    int n = str.length();     // Compute sum of even and odd digit    // sums    int oddDigSum = 0, evenDigSum = 0;    for (int i=0; i<n; i++)    {        // When i is even, position of digit is odd        if (i%2 == 0)            oddDigSum += (str[i]-'0');        else            evenDigSum += (str[i]-'0');    }     // Check its difference is divisible by 11 or not    return ((oddDigSum - evenDigSum) % 11 == 0);} // Driver codeint main(){    string str = "76945";    check(str)?  cout << "Yes" : cout << "No ";    return 0;}
// Java program to find if a number is// divisible by 11 or notclass IsDivisible{    // Function to find that number divisible by 11 or not    static boolean check(String str)    {        int n = str.length();              // Compute sum of even and odd digit        // sums        int oddDigSum = 0, evenDigSum = 0;        for (int i=0; i<n; i++)        {            // When i is even, position of digit is odd            if (i%2 == 0)                oddDigSum += (str.charAt(i)-'0');            else                evenDigSum += (str.charAt(i)-'0');        }              // Check its difference is divisible by 11 or not        return ((oddDigSum - evenDigSum) % 11 == 0);    }         // main function    public static void main (String[] args)    {        String str = "76945";        if(check(str))            System.out.println("Yes");        else            System.out.println("No");    }}
# Python 3 code program to find if a number# is divisible by 11 or not  # Function to find that number divisible by#  11 or notdef check(st) :    n = len(st)     # Compute sum of even and odd digit    # sums    oddDigSum = 0    evenDigSum = 0    for i in range(0,n) :        # When i is even, position of digit is odd        if (i % 2 == 0) :            oddDigSum = oddDigSum + ((int)(st[i]))        else:            evenDigSum = evenDigSum + ((int)(st[i]))              # Check its difference is divisible by 11 or not    return ((oddDigSum - evenDigSum) % 11 == 0) # Driver codest = "76945"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 11 or notusing System; class GFG{    // Function to find that number    // divisible by 11 or not    static bool check(string str)    {        int n = str.Length;             // Compute sum of even and odd digit        // sums        int oddDigSum = 0, evenDigSum = 0;                 for (int i = 0; i < n; i++)        {            // When i is even, position of            // digit is odd            if (i % 2 == 0)                oddDigSum += (str[i] - '0');            else                evenDigSum += (str[i] - '0');        }             // Check its difference is        // divisible by 11 or not        return ((oddDigSum - evenDigSum)                                % 11 == 0);    }         // main function    public static void Main ()    {        String str = "76945";                 if(check(str))            Console.WriteLine("Yes");        else            Console.WriteLine("No");    }} // This code is contributed by vt_m.
<?php// PHP program to find if a// number is divisible by// 11 or not // Function to find that number// divisible by 11 or notfunction check($str){    $n = strlen($str);     // Compute sum of even    // and odd digit sums    $oddDigSum = 0; $evenDigSum = 0;    for ($i = 0; $i < $n; $i++)    {                 // When i is even, position        // of digit is odd        if ($i % 2 == 0)            $oddDigSum += ($str[$i] - '0');        else            $evenDigSum += ($str[$i] - '0');    }     // Check its difference    // is divisible by 11 or not    return (($oddDigSum - $evenDigSum)                            % 11 == 0);} // Driver code$str = "76945";$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 11 or not    function check(str)    {        let n = str.length;             // Compute sum of even and odd digit        // sums        let oddDigSum = 0, evenDigSum = 0;                 for (let i = 0; i < n; i++)        {                     // When i is even, position of            // digit is odd            if (i % 2 == 0)                oddDigSum += (str[i] - '0');            else                evenDigSum += (str[i] - '0');        }             // Check its difference is        // divisible by 11 or not        return ((oddDigSum - evenDigSum)                                % 11 == 0);    }     // Driver Code    let str = "76945";    if(check(str))        document.write("Yes");    else        document.write("No");         // This code is contributed by chinmoy1997pal.</script>
Output:
Yes
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. 
jit_t
ManasChhabra2
chinmoy1997pal
divisibility
large-numbers
number-digits
Mathematical
Strings
Strings
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Program to find GCD or HCF of two numbers
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find sum of elements in a given array
Reverse a string in Java
Write a program to reverse an array or string
Longest Common Subsequence | DP-4
Check for Balanced Brackets in an expression (well-formedness) using Stack
Python program to check if a string is palindrome or not