# Check if a number is divisible by 31 or not

Given a number N, the task is to check whether the number is divisible by 31 or not.

Examples:

Input: N = 1922
Output: Yes
Explanation:
31 * 62 = 1922

Input: N = 2722400
Output: No

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The divisibility test of 31 is:

1. Extract the last digit.
2. Subtract 3 * last digit from the remaining number obtained after removing the last digit.
3. Repeat the above steps until a two-digit number, or zero, is obtained.
4. If the two-digit number is divisible by 31, or it is 0, then the original number is also divisible by 31.

For example:

```If N = 49507

Step 1:
N = 49507
Last digit = 7
Remaining number = 4950
Subtracting 3 times last digit
Resultant number = 4950 - 3*7 = 4929

Step 2:
N = 4929
Last digit = 9
Remaining number = 492
Subtracting 3 times last digit
Resultant number = 492 - 3*9 = 465

Step 3:
N = 465
Last digit = 5
Remaining number = 46
Subtracting 3 times last digit
Resultant number = 46 - 3*5 = 31

Step 4:
N = 31
Since N is a two-digit number,
and 31 is divisible by 31

Therefore N = 49507 is also divisible by 31
```

Below is the implementation of the above approach:

## C++

 `// C++ program to check whether a number ` `// is divisible by 31 or not ` `#include ` `#include ` ` `  `using` `namespace` `std; ` ` `  `// Function to check if the number is divisible by 31 or not  ` `bool` `isDivisible(``int` `n)  ` `{ ` `    ``int` `d; ` `     `  `    ``// While there are at least two digits  ` `    ``while` `(n / 100)  ` `    ``{ ` ` `  `        ``// Extracting the last  ` `        ``d = n % 10; ` ` `  `        ``// Truncating the number  ` `        ``n /= 10; ` ` `  `        ``// Subtracting three times the last  ` `        ``// digit to the remaining number  ` `        ``n = ``abs``(n-(d * 3)); ` `    ``} ` `     `  `    ``// Finally return if the two-digit ` `    ``// number is divisible by 31 or not ` `    ``return` `(n % 31 == 0) ; ` `} ` ` `  `// Driver Code  ` `int` `main()  ` `{ ` `    ``int` `N = 1922; ` ` `  `    ``if` `(isDivisible(N))  ` `        ``cout<<``"Yes"``<

## Java

 `// Java program to check whether a number ` `// is divisible by 31 or not ` `import` `java.util.*; ` ` `  `class` `GFG{ ` `  `  `// Function to check if the number is divisible by 31 or not  ` `static` `boolean` `isDivisible(``int` `n)  ` `{ ` `    ``int` `d; ` `      `  `    ``// While there are at least two digits  ` `    ``while` `((n / ``100``) > ``0``)  ` `    ``{ ` `  `  `        ``// Extracting the last  ` `        ``d = n % ``10``; ` `  `  `        ``// Truncating the number  ` `        ``n /= ``10``; ` `  `  `        ``// Subtracting three times the last  ` `        ``// digit to the remaining number  ` `        ``n = Math.abs(n - (d * ``3``)); ` `    ``} ` `      `  `    ``// Finally return if the two-digit ` `    ``// number is divisible by 31 or not ` `    ``return` `(n % ``31` `== ``0``) ; ` `} ` `  `  `// Driver Code  ` `public` `static` `void` `main(String[] args)  ` `{ ` `    ``int` `N = ``1922``; ` `  `  `    ``if` `(isDivisible(N))  ` `        ``System.out.print(``"Yes"``); ` `    ``else` `        ``System.out.print(``"No"``); ` `}      ` `}  ` ` `  `// This code is contributed by PrinciRaj1992 `

## Python 3

 `# Python program to check whether a number ` `# is divisible by 31 or not ` ` `  `# Function to check if the number is  ` `# divisible by 31 or not  ` `def` `isDivisible(n) :  ` ` `  `    ``# While there are at least two digits  ` `    ``while` `n ``/``/` `100` `:  ` ` `  `        ``# Extracting the last  ` `        ``d ``=` `n ``%` `10` ` `  `        ``# Truncating the number  ` `        ``n ``/``/``=` `10` ` `  `        ``# Subtracting three times the last  ` `        ``# digit to the remaining number  ` `        ``n ``=` `abs``(n``-``(d ``*` `3``)) ` ` `  `    ``# Finally return if the two-digit ` `    ``# number is divisible by 31 or not ` `    ``return` `(n ``%` `31` `=``=` `0``)  ` ` `  `# Driver Code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` ` `  `    ``n ``=` `1922` ` `  `    ``if` `(isDivisible(n)) :  ` `        ``print``(``"Yes"``)  ` `    ``else` `:  ` `        ``print``(``"No"``)  `

## C#

 `// C# program to check whether a number ` `// is divisible by 31 or not ` `using` `System; ` ` `  `class` `GFG{ ` `   `  `// Function to check if the number is divisible by 31 or not  ` `static` `bool` `isDivisible(``int` `n)  ` `{ ` `    ``int` `d; ` `       `  `    ``// While there are at least two digits  ` `    ``while` `((n / 100) > 0)  ` `    ``{ ` `   `  `        ``// Extracting the last  ` `        ``d = n % 10; ` `   `  `        ``// Truncating the number  ` `        ``n /= 10; ` `   `  `        ``// Subtracting three times the last  ` `        ``// digit to the remaining number  ` `        ``n = Math.Abs(n - (d * 3)); ` `    ``} ` `       `  `    ``// Finally return if the two-digit ` `    ``// number is divisible by 31 or not ` `    ``return` `(n % 31 == 0) ; ` `} ` `   `  `// Driver Code  ` `public` `static` `void` `Main(String[] args)  ` `{ ` `    ``int` `N = 1922; ` `   `  `    ``if` `(isDivisible(N))  ` `        ``Console.Write(``"Yes"``); ` `    ``else` `        ``Console.Write(``"No"``); ` `}      ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output:

```Yes
```

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.