# Check a large number is divisible by 16 or not

Given a number, the task is to check if a number is divisible by 16 or not. The input number may be large and it may not be possible to store even if we use long long int.

Examples:

```Input  : n = 1128
Output : No

Input  : n = 11216
Output : Yes

Input  : n = 1124273542764284287
Output : No
```

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

Since input number may be very large, we cannot use n % 16 to check if a number is divisible by 16 or not, especially in languages like C/C++. The idea is based on following fact.

```A number is divisible by 16 if number formed
by last four digits of it is divisible by 16.```

Illustration:

```For example, let us consider 769616
Number formed by last four digits = 9616
Since 9522 is divisible by 16, answer is YES.
```

How does this work?

```Let us consider 76952, we can write it as
76942 = 7*10000 + 6*1000 + 9*100 + 5*10 + 2

The proof is based on below observation:
Remainder of 10i divided by 16 is 0 if i greater
than or equal to four. Note that 10000,
100000,... etc lead to remainder 0 when divided by 16.

So remainder of "7*10000 + 6*1000 + 9*100 +
5*10 + 2" divided by 16 is equivalent to remainder
of following :
0 + 6*1000 + 9*100 + 5*10 + 2 = 6952
Therefore we can say that the whole number is
divisible by 16 if 6952 is divisible by 16.
```

## C++

 `// C++ program to find if a number ` `// is divisible by 16 or not ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find that ` `// number divisible by 16 or not ` `bool` `check(string str) ` `{ ` `    ``int` `n = str.length(); ` ` `  `    ``// Empty string ` `    ``if` `(n == 0 && n == 1) ` `        ``return` `false``; ` ` `  `    ``// If there is double digit ` `    ``if` `(n == 2) ` `        ``return` `(((str[n-2]-``'0'``)*10 + ` `                 ``(str[n-1]-``'0'``))%16 == 0); ` ` `  `    ``// If there is triple digit ` `    ``if``(n == 3) ` `         ``return` `( ((str[n-3]-``'0'``)*100 + ` `                   ``(str[n-2]-``'0'``)*10 + ` `                   ``(str[n-1]-``'0'``))%16 == 0); ` ` `  ` `  `    ``// If number formed by last four ` `    ``// digits is divisible by 16. ` `    ``int` `last = str[n-1] - ``'0'``; ` `    ``int` `second_last = str[n-2] - ``'0'``; ` `    ``int` `third_last = str[n-3] - ``'0'``; ` `    ``int` `fourth_last = str[n-4] - ``'0'``; ` `    ``return` `((fourth_last*1000 + third_last*100 + ` `             ``second_last*10 + last) % 16 == 0); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string str = ``"769528"``; ` `    ``check(str)?  cout << ``"Yes"` `: cout << ``"No "``; ` `    ``return` `0; ` `} `

## Java

 `// Java program to find if a number ` `// is divisible by 16 or not ` `import` `java.io.*; ` ` `  `class` `GFG { ` `    ``// Function to find that ` `    ``// number divisible by 16 or not ` `    ``static` `boolean` `check(String str) ` `    ``{ ` `        ``int` `n = str.length(); ` `      `  `        ``// Empty string ` `        ``if` `(n == ``0` `&& n == ``1``) ` `            ``return` `false``; ` `      `  `        ``// If there is double digit ` `        ``if` `(n == ``2``) ` `            ``return` `(((str.charAt(n-``2``)-``'0'``)*``10` `+ ` `                     ``(str.charAt(n-``1``)-``'0'``))%``16` `== ``0``); ` `      `  `        ``// If there is triple digit ` `        ``if``(n == ``3``) ` `             ``return` `( ((str.charAt(n-``3``)-``'0'``)*``100` `+ ` `                       ``(str.charAt(n-``2``)-``'0'``)*``10` `+ ` `                       ``(str.charAt(n-``1``)-``'0'``))%``16` `== ``0``); ` `      `  `      `  `        ``// If number formed by last ` `        ``// four digits is divisible by 16. ` `        ``int` `last = str.charAt(n-``1``) - ``'0'``; ` `        ``int` `second_last = str.charAt(n-``2``) - ``'0'``; ` `        ``int` `third_last = str.charAt(n-``3``) - ``'0'``; ` `        ``int` `fourth_last = str.charAt(n-``4``) - ``'0'``; ` `        ``return` `((fourth_last*``1000` `+ third_last*``100`  `                ``+ second_last*``10` `+ last) % ``16` `== ``0``); ` `    ``} ` `      `  `    ``// Driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``String str = ``"769528"``; ` `        ``if``(check(str)) ` `            ``System.out.println(``"Yes"``); ` `        ``else` `            ``System.out.println(``"No "``); ` `    ``} ` `} ` ` `  `// This code is contributed by Nikita Tiwari. `

## Python3

 `# Python 3 program to find ` `# if a number is divisible ` `# by 16 or not ` ` `  `# Function to find that ` `# number divisible by ` `# 16 or not ` `def` `check(st) : ` `    ``n ``=` `len``(st)  ` `     `  `    ``# Empty string ` `    ``if` `(n ``=``=` `0` `and` `n ``=``=` `1``) : ` `        ``return` `False` `  `  `    ``# If there is double digit ` `    ``if` `(n ``=``=` `2``) : ` `        ``return` `((``int``)(st[n``-``2``])``*``10` `+` `                ``((``int``)(st[n``-``1``])``%``16` `=``=` `0``)) ` `  `  `    ``# If there is triple digit ` `    ``if``(n ``=``=` `3``) : ` `        ``return` `( ((``int``)(st[n``-``3``])``*``100` `+` `                   ``(``int``)(st[n``-``2``])``*``10` `+` `                   ``(``int``)(st[n``-``1``]))``%``16` `=``=` `0``) ` `  `  `  `  `    ``# If number formed by last ` `    ``# four digits is divisible ` `    ``# by 16. ` `    ``last ``=` `(``int``)(st[n``-``1``]) ` `    ``second_last ``=` `(``int``)(st[n``-``2``]) ` `    ``third_last ``=` `(``int``)(st[n``-``3``]) ` `    ``fourth_last ``=` `(``int``)(st[n``-``4``]) ` `    ``return` `((fourth_last``*``1000` `+` `third_last``*``100` `            ``+` `second_last``*``10` `+` `last) ``%` `16` `=``=` `0``) ` ` `  ` `  `# Driver code ` `st ``=` `"769528"` `if``(check(st)) : ` `    ``print``(``"Yes"``) ` `else` `: ` `    ``print``(``"No"``) ` `     `  ` `  `# This code is conributed by Nikita Tiwari. `

## C#

 `// C# program to find if a number ` `// is divisible by 16 or not ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// Function to find that number  ` `    ``// divisible by 16 or not ` `    ``static` `bool` `check(String str) ` `    ``{ ` `        ``int` `n = str.Length; ` `     `  `        ``// Empty string ` `        ``if` `(n == 0 && n == 1) ` `            ``return` `false``; ` `     `  `        ``// If there is double digit ` `        ``if` `(n == 2) ` `            ``return` `(((str[n - 2] - ``'0'``) * 10 + ` `                ``(str[n - 1] - ``'0'``)) % 16 == 0); ` `     `  `        ``// If there is triple digit ` `        ``if``(n == 3) ` `            ``return` `(((str[n - 3] - ``'0'``) * 100 + ` `                     ``(str[n - 2] - ``'0'``) * 10 + ` `                     ``(str[n - 1] - ``'0'``)) % 16 == 0); ` `     `  `     `  `        ``// If number formed by last ` `        ``// four digits is divisible by 16. ` `        ``int` `last = str[n - 1] - ``'0'``; ` `        ``int` `second_last = str[n - 2] - ``'0'``; ` `        ``int` `third_last = str[n - 3] - ``'0'``; ` `        ``int` `fourth_last = str[n - 4] - ``'0'``; ` `        ``return` `((fourth_last * 1000 + third_last * 100 ` `            ``+ second_last * 10 + last) % 16 == 0); ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``String str = ``"769528"``; ` `        ``if``(check(str)) ` `            ``Console.Write(``"Yes"``); ` `        ``else` `            ``Console.Write(``"No "``); ` `    ``} ` `} ` ` `  `// This code is contributed by Nitin Mittal. `

## PHP

 ` `

Output:

```No
```

This article is contributed by Danish_Raza. 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.

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

Improved By : nitin mittal, jit_t

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.