# Check if a number is divisible by 23 or not

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

**Examples:**

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

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

**Approach:**

- Extract the last digit of the number/truncated number every time
- Add 7*(last digit of the previous number) to the truncated number
- Repeat the above three steps as long as necessary.

Illustration:

17043-->1704+7*3 = 1725-->172+7*5 = 207 which is 9*23, so 17043 is also divisible by 23.

Mathematical Proof :

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

Now assume that is divisible by 23. Then

0 (mod 23)

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

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

10+c 0 (mod 23)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 23.

It can be observed that the smallest n which satisfies this property is 7 as 701 mod 23.

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

by 7 and simplify it:

70+7c 0 (mod 23)

+7c 0 (mod 23)

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

+7c 0 (mod 23).

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

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

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

## C++

`// CPP program to validate above logic ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to check if the number is ` `// divisible by 23 or not ` `bool` `isDivisible(` `long` `long` `int` `n) ` `{ ` ` ` ` ` `// While there are at least 3 digits ` ` ` `while` `(n / 100) ` ` ` `{ ` ` ` `int` `d = n % 10; ` `// Extracting the last digit ` ` ` `n /= 10; ` `// Truncating the number ` ` ` ` ` `// Adding seven times the last ` ` ` `// digit to the remaining number ` ` ` `n += d * 7; ` ` ` `} ` ` ` ` ` `return` `(n % 23 == 0); ` `} ` ` ` `int` `main() ` `{ ` ` ` `long` `long` `int` `n = 1191216; ` ` ` `if` `(isDivisible(n)) ` ` ` `cout << ` `"Yes"` `<< endl; ` ` ` `else` ` ` `cout << ` `"No"` `<< endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to validate above logic ` `class` `GFG ` `{ ` ` ` `// Function to check if the ` `// number is divisible by ` `// 23 or not ` `static` `boolean` `isDivisible(` `long` `n) ` `{ ` ` ` ` ` `// While there are at ` ` ` `// least 3 digits ` ` ` `while` `(n / ` `100` `!= ` `0` `) ` ` ` `{ ` ` ` `// Extracting the last digit ` ` ` `long` `d = n % ` `10` `; ` ` ` ` ` `n /= ` `10` `; ` `// Truncating the number ` ` ` ` ` `// Adding seven times the last ` ` ` `// digit to the remaining number ` ` ` `n += d * ` `7` `; ` ` ` `} ` ` ` ` ` `return` `(n % ` `23` `== ` `0` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `long` `n = ` `1191216` `; ` ` ` `if` `(isDivisible(n)) ` ` ` `System.out.println(` `"Yes"` `); ` ` ` `else` ` ` `System.out.println(` `"No"` `); ` `} ` `} ` ` ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to validate above logic ` ` ` `# Function to check if the number is ` `# divisible by 23 or not ` `def` `isDivisible(n) : ` ` ` ` ` `# While there are at least 3 digits ` ` ` `while` `n ` `/` `/` `100` `: ` ` ` ` ` `# Extracting the last ` ` ` `d ` `=` `n ` `%` `10` ` ` ` ` `# Truncating the number ` ` ` `n ` `/` `/` `=` `10` ` ` ` ` `# Adding seven times the last ` ` ` `# digit to the remaining number ` ` ` `n ` `+` `=` `d ` `*` `7` ` ` ` ` `return` `(n ` `%` `23` `=` `=` `0` `) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `1191216` ` ` ` ` `# function calling ` ` ` `if` `(isDivisible(n)) : ` ` ` `print` `(` `"Yes"` `) ` ` ` ` ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# This code is contributed by ANKITRAI1 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to validate ` `// above logic ` `class` `GFG ` `{ ` ` ` `// Function to check if the ` `// number is divisible by ` `// 23 or not ` `static` `bool` `isDivisible(` `long` `n) ` `{ ` ` ` ` ` `// While there are at ` ` ` `// least 3 digits ` ` ` `while` `(n / 100 != 0) ` ` ` `{ ` ` ` `// Extracting the last digit ` ` ` `long` `d = n % 10; ` ` ` ` ` `n /= 10; ` `// Truncating the number ` ` ` ` ` `// Adding seven times the last ` ` ` `// digit to the remaining number ` ` ` `n += d * 7; ` ` ` `} ` ` ` ` ` `return` `(n % 23 == 0); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `long` `n = 1191216; ` ` ` `if` `(isDivisible(n)) ` ` ` `System.Console.WriteLine(` `"Yes"` `); ` ` ` `else` ` ` `System.Console.WriteLine(` `"No"` `); ` `} ` `} ` ` ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to validate above logic ` ` ` `// Function to check if the number ` `// is divisible by 23 or not ` `function` `isDivisible(` `$n` `) ` `{ ` ` ` ` ` `// While there are at ` ` ` `// least 3 digits ` ` ` `while` `(` `intval` `(` `$n` `/ 100)) ` ` ` `{ ` ` ` `$n` `= ` `intval` `(` `$n` `); ` ` ` `$d` `= ` `$n` `% 10; ` `// Extracting the last digit ` ` ` `$n` `/= 10; ` `// Truncating the number ` ` ` ` ` `// Adding seven times the last ` ` ` `// digit to the remaining number ` ` ` `$n` `+= ` `$d` `* 7; ` ` ` `} ` ` ` ` ` `return` `(` `$n` `% 23 == 0); ` `} ` ` ` `$n` `= 1191216; ` `if` `(isDivisible(` `$n` `)) ` `echo` `"Yes"` `. ` `"\n"` `; ` `else` `echo` `"No"` `. ` `"\n"` `; ` ` ` `// This code is contributed ` `// by ChitraNayal ` `?> ` |

*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.

## Recommended Posts:

- Check if a number is divisible by 41 or not
- Check if a number is divisible by all prime divisors of another number
- Check if a large number is divisible by 2, 3 and 5 or not
- Check if any large number is divisible by 17 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 any large number is divisible by 19 or not
- To check whether a large number is divisible by 7
- Check if a large number is divisible by 3 or not
- Check if a large number is divisible by 4 or not
- Check if a large number is divisible by 8 or not
- Check if a large number is divisible by 11 or not
- Check if a large number is divisible by 6 or not
- Check if a large number is divisible by 5 or not
- Check if a large number is divisible by 9 or not

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.