# Check if a large number is divisible by 13 or not

• Difficulty Level : Easy
• Last Updated : 05 Jun, 2022

Given a large number, the task is to check if number is divisible by 13 or not.

Examples :

```Input :  637
Output : 637 is divisible by 13.

Input :  920
Output : 920 is not divisible by 13.

Input  : 83959092724
Output : 83959092724 is divisible by 13.```

If given number num is small, we can easily find whether it is divisible by 13 or not by doing num % 13 and checking whether the result is 0 or not. But what about very large numbers. Let’s discuss for large numbers.

Below are some interesting facts about divisibility of 13.

• A number is divisible by 13 if and if alternating sum (alternatively adding and subtracting) of blocks of three from right to left is divisible by 13. For example 2911285 is divisible by 13 because alternating sum of blocks of size 3 is 2 – 911 + 285 = -650 which is divisible by 13.
• A number is divisible by 13 if and only if the number obtained by adding last digit multiplied by 4 to rest is also divisible by 13.
For example consider 2353. Applying above rule, we get 235 + 3*4 = 247. Again we apply rule and get, 24 + 7*4 = 52. Since 52 is divisible by 13, given number is divisible by 13.

Below is implementation based one first fact above (Finding alternating sum of blocks of size 3)

## C++

 `// CPP program to check``// whether a number is``// divisible by 13 or not.``#include ``using` `namespace` `std;` `// Returns true if number``// is divisible by 13 else``// returns false``bool` `checkDivisibility(string num)``{``    ``int` `length = num.size();``    ``if` `(length == 1 && num[0] == ``'0'``)``        ``return` `true``;` `    ``// Append required 0s .``    ``// at the beginning.``    ``if` `(length % 3 == 1)``    ``{``        ``// Same as strcat(num, "00");``        ``// in c.``        ``num +=``"00"``;``        ``length += 2;``    ``}``    ``else` `if` `(length % 3 == 2)``    ``{``        ``// Same as strcat(num, "0");``        ``// in c.``        ``num += ``"0"``;``        ``length += 1;``    ``}` `    ``// Alternatively add/subtract``    ``// digits in group of three``    ``// to result.``    ``int` `sum = 0, p = 1;``    ``for` `(``int` `i = length - 1; i >= 0; i--)``    ``{``        ``// Store group of three``        ``// numbers in group variable.``        ``int` `group = 0;``        ``group += num[i--] - ``'0'``;``        ``group += (num[i--] - ``'0'``) * 10;``        ``group += (num[i] - ``'0'``) * 100;` `        ``sum = sum + group * p;` `        ``// Generate alternate series``        ``// of plus and minus``        ``p *= (-1);``    ``}``    ``sum = ``abs``(sum);``    ``return` `(sum % 13 == 0);``}` `// Driver code``int` `main()``{``    ``string number = ``"83959092724"``;``    ``if` `(checkDivisibility(number))``        ``cout << number << ``" is divisible by 13."``;``    ``else``        ``cout << number << ``" is not divisible by 13."``;``    ``return` `0;``}`

## Java

 `// Java program to check``// whether a number is``// divisible by 13 or not.` `class` `GFG``{``    ` `// Returns true if number``// is divisible by 13 else``// returns false``static` `boolean` `checkDivisibility(String num)``{``    ``int` `length = num.length();``    ``if` `(length == ``1` `&&``        ``num.charAt(``0``) == ``'0'``)``        ``return` `true``;` `    ``// Append required 0s .``    ``// at the beginning.``    ``if` `(length % ``3` `== ``1``)``    ``{``        ``// Same as strcat(num, "00");``        ``// in c.``        ``num +=``"00"``;``        ``length += ``2``;``    ``}``    ``else` `if` `(length % ``3` `== ``2``)``    ``{``        ``// Same as strcat(num, "0");``        ``// in c.``        ``num += ``"0"``;``        ``length += ``1``;``    ``}` `    ``// Alternatively add/subtract``    ``// digits in group of three``    ``// to result.``    ``int` `sum = ``0``, p = ``1``;``    ``for` `(``int` `i = length - ``1``; i >= ``0``; i--)``    ``{``        ``// Store group of three``        ``// numbers in group variable.``        ``int` `group = ``0``;``        ``group += num.charAt(i--) - ``'0'``;``        ``group += (num.charAt(i--) - ``'0'``) * ``10``;``        ``group += (num.charAt(i) - ``'0'``) * ``100``;` `        ``sum = sum + group * p;` `        ``// Generate alternate series``        ``// of plus and minus``        ``p *= (-``1``);``    ``}``    ``sum = Math.abs(sum);``    ``return` `(sum % ``13` `== ``0``);``}` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``String number = ``"83959092724"``;``    ` `    ``if` `(checkDivisibility(number))``            ``System.out.println(number +``                       ``" is divisible by 13."``);``        ``else``            ``System.out.println(number +``                       ``" is not divisible by 13."``);``}``}` `// This code is contributed by mits`

## C#

 `// C# program to check``// whether a number is``// divisible by 13 or not.``using` `System;` `class` `GFG {``    ` `    ``// Returns true if number``    ``// is divisible by 13 else``    ``// returns false``    ``static` `bool` `checkDivisibility(``string` `num)``    ``{``        ``int` `length = num.Length;``        ``if` `(length == 1 && num[0] == ``'0'``)``            ``return` `true``;``    ` `        ``// Append required 0s .``        ``// at the beginning.``        ``if` `(length % 3 == 1)``        ``{``            ``// Same as strcat(num, "00");``            ``// in c.``            ``num +=``"00"``;``            ``length += 2;``        ``}``        ``else` `if` `(length % 3 == 2)``        ``{``            ``// Same as strcat(num, "0");``            ``// in c.``            ``num += ``"0"``;``            ``length += 1;``        ``}` `        ``// Alternatively add/subtract``        ``// digits in group of three``        ``// to result.``        ``int` `sum = 0, p = 1;``        ``for` `(``int` `i = length - 1; i >= 0; i--)``        ``{``            ``// Store group of three``            ``// numbers in group variable.``            ``int` `group` `= 0;``            ``group` `+= num[i--] - ``'0'``;``            ``group` `+= (num[i--] - ``'0'``) * 10;``            ``group` `+= (num[i] - ``'0'``) * 100;``    ` `            ``sum = sum + ``group` `* p;``    ` `            ``// Generate alternate series``            ``// of plus and minus``            ``p *= (-1);``        ``}``        ``sum = Math.Abs(sum);``        ``return` `(sum % 13 == 0);``    ``}``    ` `    ``// Driver code``    ``static` `void` `Main()``    ``{``        ``string` `number = ``"83959092724"``;``        ` `        ``if` `(checkDivisibility(number))``                ``Console.Write( number +``                    ``" is divisible by 13."``);``            ``else``                ``Console.Write( number +``                  ``" is not divisible by 13."``);``    ``}``}` `// This code is contributed by Sam007`

## PHP

 `= 0; ``\$i``--)``    ``{``        ``// Store group of three``        ``// numbers in group variable.``        ``\$group` `= 0;``        ``\$group` `+= ``\$num``[``\$i``--] - ``'0'``;``        ``\$group` `+= (``\$num``[``\$i``--] - ``'0'``) * 10;``        ``\$group` `+= (``\$num``[``\$i``] - ``'0'``) * 100;` `        ``\$sum` `= ``\$sum` `+ ``\$group` `* ``\$p``;` `        ``// Generate alternate series ``        ``// of plus and minus``        ``\$p` `*= (-1);``    ``}``    ` `    ``\$sum` `= ``abs``(``\$sum``);``    ``return` `(``\$sum` `% 13 == 0);``}` `// Driver code``\$number` `= ``"83959092724"``;``if` `(checkDivisibility(``\$number``))``    ``echo``(``\$number` `. ``" is divisible by 13."``);``else``    ``echo``(``\$number` `. ``" is not divisible by 13."``);` `// This code is contributed by Ajit.``?>`

## Javascript

 ``

Output

`83959092724 is divisible by 13.`

Method: Checking given number is divisible by 13 or not by using the modulo division operator “%”.

## Python3

 `# Python code``# To check whether the given number is divisible by 13 or not` `#input``n``=``83959092724``# the above input can also be given as n=input() -> taking input from user``# finding given number is divisible by 13 or not``if` `int``(n)``%``13``=``=``0``:``  ``print``(``"Yes"``)``else``:``  ``print``(``"No"``)` `  ``# this code is contributed by gangarajula laxmi`

Output

```Yes
```

My Personal Notes arrow_drop_up