Given a number, the task is to quickly check if the number is divisible by 17 or not.

**Example:**

Input : x = 34 Output : Yes Input : x = 47 Output : No

A solution to the problem is to extract the last digit and subtract 5 times of last digit from remaining number and repeat this process until a two digit number is obtained. If the obtained two digit number is divisible by 17, then the given number is divisible by 17.

**Approach:**

- Extract the last digit of the number/truncated number every time
- Substract 5*(last digit of the previous number) from the truncated number
- Repeat the above three steps as long as necessary.

**Illustration:**

3978-->397-5*8=357-->35-5*7=0. So 3978 is divisible by 17.

Mathematical Proof :

Let be any number such that =100a+10b+c .

Now assume that is divisible by 17. Then

0 (mod 17)

100a+10b+c 0 (mod 17)

10(10a+b)+c 0 (mod 17)

10+c 0 (mod 17)

Now that we have separated the last digit from the number, we have to find a way to use it.

Make the coefficient of 1.

In other words, we have to find an integer such that n such that 10n1 mod 17.

It can be observed that the smallest n which satisfies this property is -5 as -501 mod 17.

Now we can multiply the original equation 10+c 0 (mod 17)

by -5 and simplify it:

-50-5c 0 (mod 17)

-5c 0 (mod 17)

We have found out that if 0 (mod 17) then,

-5c 0 (mod 17).

In other words, to check if a 3-digit number is divisible by 17,

we can just remove the last digit, multiply it by 5,

and then subtract it from the rest of the two digits.

**Program :**

## C++

`// CPP Program to validate the above logic ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// Function to check if the ` `// number is divisible by 17 or not ` `bool` `isDivisible(` `long` `long` `int` `n) ` `{ ` ` ` ` ` `while` `(n / 100) ` ` ` `{ ` ` ` `// Extracting the last digit ` ` ` `int` `d = n % 10; ` ` ` ` ` `// Truncating the number ` ` ` `n /= 10; ` ` ` ` ` `// Subtracting the five times the ` ` ` `// last digit from the remaining number ` ` ` `n -= d * 5; ` ` ` `} ` ` ` ` ` `// Return n is divisible by 17 ` ` ` `return` `(n % 17 == 0); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `long` `long` `int` `n = 19877658; ` ` ` `if` `(isDivisible(n)) ` ` ` `cout << ` `"Yes"` `<< endl; ` ` ` `else` ` ` `cout << ` `"No"` `<< endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to validate the above logic ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to check if the ` `// number is divisible by 17 or not ` ` ` `static` `boolean` `isDivisible(` `long` `n) ` `{ ` ` ` ` ` `while` `(n / ` `100` `>` `0` `) ` ` ` `{ ` ` ` `// Extracting the last digit ` ` ` `long` `d = n % ` `10` `; ` ` ` ` ` `// Truncating the number ` ` ` `n /= ` `10` `; ` ` ` ` ` `// Subtracting the five times the ` ` ` `// last digit from the remaining number ` ` ` `n -= d * ` `5` `; ` ` ` `} ` ` ` ` ` `// Return n is divisible by 17 ` ` ` `return` `(n % ` `17` `== ` `0` `); ` `} ` ` ` `// Driver code ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `long` `n = ` `19877658` `; ` ` ` `if` `(isDivisible(n)) ` ` ` `System.out.println( ` `"Yes"` `); ` ` ` `else` ` ` `System.out.println( ` `"No"` `); ` ` ` `} ` `} ` `// This code is contributed by inder_verma. ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 Program to validate ` `# the above logic ` ` ` `# Function to check if the ` `# number is divisible by 17 or not ` `def` `isDivisible(n) : ` ` ` ` ` `while` `(n ` `/` `/` `100` `) : ` ` ` ` ` `# Extracting the last digit ` ` ` `d ` `=` `n ` `%` `10` ` ` ` ` `# Truncating the number ` ` ` `n ` `/` `/` `=` `10` ` ` ` ` `# Subtracting the five times ` ` ` `# the last digit from the ` ` ` `# remaining number ` ` ` `n ` `-` `=` `d ` `*` `5` ` ` ` ` `# Return n is divisible by 17 ` ` ` `return` `(n ` `%` `17` `=` `=` `0` `) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `19877658` ` ` ` ` `if` `isDivisible(n) : ` ` ` `print` `(` `"Yes"` `) ` ` ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# This code is contributed ` `# by ANKITRAI1 ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to validate the above logic ` ` ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to check if the ` `// number is divisible by 17 or not ` ` ` `static` `bool` `isDivisible(` `long` `n) ` `{ ` ` ` ` ` `while` `(n / 100>0) ` ` ` `{ ` ` ` `// Extracting the last digit ` ` ` `long` `d = n % 10; ` ` ` ` ` `// Truncating the number ` ` ` `n /= 10; ` ` ` ` ` `// Subtracting the five times the ` ` ` `// last digit from the remaining number ` ` ` `n -= d * 5; ` ` ` `} ` ` ` ` ` `// Return n is divisible by 17 ` ` ` `return` `(n % 17 == 0); ` `} ` ` ` `// Driver code ` ` ` ` ` `public` `static` `void` `Main () { ` ` ` `long` `n = 19877658; ` ` ` `if` `(isDivisible(n)) ` ` ` `Console.Write( ` `"Yes"` `); ` ` ` `else` ` ` `Console.Write( ` `"No"` `); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to validate the above logic ` ` ` `// Function to check if the ` `// number is divisible by 17 or not ` `function` `isDivisible(` `$n` `) ` `{ ` ` ` ` ` `while` `(` `$n` `/ 100 != 0) ` ` ` `{ ` ` ` `// Extracting the last digit ` ` ` `$d` `= (int)` `$n` `% 10; ` ` ` ` ` `// Truncating the number ` ` ` `$n` `/= 10; ` ` ` ` ` `// Subtracting the five times ` ` ` `// the last digit from the ` ` ` `// remaining number ` ` ` `$n` `-= ` `$d` `* 5; ` ` ` `} ` ` ` ` ` `// Return n is divisible by 17 ` ` ` `return` `(` `$n` `% 17 == 0); ` `} ` ` ` `// Driver code ` `$n` `= 19877658; ` `if` `(isDivisible(` `$n` `)) ` ` ` `print` `(` `"Yes"` `); ` `else` ` ` `print` `(` `"No"` `); ` ` ` `// This code is contributed by Raj ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Yes

Note that the above program may not make a lot of sense as could simply do n % 23 to check for divisibility. The idea of this program is to validate the concept. Also, this might be an efficient approach if input number is large and given as string.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Check if any large number is divisible by 19 or not
- Check if any permutation of a large number is divisible by 8
- Check if a large number is divisible by 11 or not
- Check if a large number is divisible by 3 or not
- Check if a large number is divisible by 8 or not
- Check if a large number is divisible by 6 or not
- Check if a large number is divisible by 9 or not
- Check if a large number is divisible by 5 or not
- Check a large number is divisible by 16 or not
- Check if a large number is divisible by 25 or not
- Check if a large number is divisible by 13 or not
- Check if the large number formed is divisible by 41 or not
- Check if a large number is divisible by 2, 3 and 5 or not
- Check if a large number is divisible by 75 or not
- Check whether a large number is divisible by 53 or not
- Check if a large number is divisible by 4 or not
- Check if a large number is divisible by a number which is a power of 2
- Check if any permutation of a number without any leading zeros is a power of 2 or not
- Number of factors of very large number N modulo M where M is any prime number
- To check divisibility of any large number by 999

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.