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

Given a number, the task is to check if a number is divisible by 4 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 = 1124 Output : Yes Input : n = 1234567589333862 Output : No Input : n = 363588395960667043875487 Output : No

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

A number is divisible by 4 if number formed by last two digits of it is divisible by 4.

**Illustration:**

For example, let us consider 76952Number formed by last two digits = 52 Since 52 is divisible by 4, 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 10^{i}divided by 4 is 0 if i greater than or equal totwo. Note than 100, 1000, ... etc lead to remainder 0 when divided by 4. So remainder of "7*10000 + 6*1000 + 9*100 + 5*10 + 2" divided by 4 is equivalent to remainder of following : 0 + 0 + 0 + 5*10 + 2 = 52 Therefore we can say that the whole number is divisible by 4 if 52 is divisible by 4.

Below is implementation of above fact :

## C++

`// C++ program to find if a number is divisible by ` `// 4 or not ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find that number divisible by ` `// 4 or not ` `bool` `check(string str) ` `{ ` ` ` `int` `n = str.length(); ` ` ` ` ` `// Empty string ` ` ` `if` `(n == 0) ` ` ` `return` `false` `; ` ` ` ` ` `// If there is single digit ` ` ` `if` `(n == 1) ` ` ` `return` `((str[0]-` `'0'` `)%4 == 0); ` ` ` ` ` `// If number formed by last two digits is ` ` ` `// divisible by 4. ` ` ` `int` `last = str[n-1] - ` `'0'` `; ` ` ` `int` `second_last = str[n-2] - ` `'0'` `; ` ` ` `return` `((second_last*10 + last) % 4 == 0); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string str = ` `"76952"` `; ` ` ` `check(str)? cout << ` `"Yes"` `: cout << ` `"No "` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find if a number is ` `// divisible by 4 or not ` `class` `IsDivisible ` `{ ` ` ` `// Function to find that number ` ` ` `// is divisible by 4 or not ` ` ` `static` `boolean` `check(String str) ` ` ` `{ ` ` ` `int` `n = str.length(); ` ` ` ` ` `// Empty string ` ` ` `if` `(n == ` `0` `) ` ` ` `return` `false` `; ` ` ` ` ` `// If there is single digit ` ` ` `if` `(n == ` `1` `) ` ` ` `return` `((str.charAt(` `0` `)-` `'0'` `)%` `4` `== ` `0` `); ` ` ` ` ` `// If number formed by last two digits is ` ` ` `// divisible by 4. ` ` ` `int` `last = str.charAt(n-` `1` `) - ` `'0'` `; ` ` ` `int` `second_last = str.charAt(n-` `2` `) - ` `'0'` `; ` ` ` `return` `((second_last*` `10` `+ last) % ` `4` `== ` `0` `); ` ` ` `} ` ` ` ` ` `// main function ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `String str = ` `"76952"` `; ` ` ` `if` `(check(str)) ` ` ` `System.out.println(` `"Yes"` `); ` ` ` `else` ` ` `System.out.println(` `"No"` `); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find ` `# if a number is divisible ` `# by 4 or not ` ` ` `# Function to find that ` `# number divisible by ` `# 4 or not ` `def` `check(st) : ` ` ` `n ` `=` `len` `(st) ` ` ` ` ` `# Empty string ` ` ` `if` `(n ` `=` `=` `0` `) : ` ` ` `return` `False` ` ` ` ` `# If there is single ` ` ` `# digit ` ` ` `if` `(n ` `=` `=` `1` `) : ` ` ` `return` `((st[` `0` `] ` `-` `'0'` `) ` `%` `4` `=` `=` `0` `) ` ` ` ` ` `# If number formed by ` ` ` `# last two digits is ` ` ` `# divisible by 4. ` ` ` `last ` `=` `(` `int` `)(st[n ` `-` `1` `]) ` ` ` `second_last ` `=` `(` `int` `)(st[n ` `-` `2` `]) ` ` ` ` ` `return` `((second_last ` `*` `10` `+` `last) ` `%` `4` `=` `=` `0` `) ` ` ` ` ` `# Driver code ` `st ` `=` `"76952"` `if` `(check(st)) : ` ` ` `print` `(` `"Yes"` `) ` `else` `: ` ` ` `print` `(` `"No "` `) ` ` ` `# This code is contributed by Nikita tiwari ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find if a number is ` `// divisible by 4 or not ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to find that number ` ` ` `// is divisible by 4 or not ` ` ` `static` `bool` `check(String str) ` ` ` `{ ` ` ` `int` `n = str.Length; ` ` ` ` ` `// Empty string ` ` ` `if` `(n == 0) ` ` ` `return` `false` `; ` ` ` ` ` `// If there is single digit ` ` ` `if` `(n == 1) ` ` ` `return` `((str[0] - ` `'0'` `) % 4 == 0); ` ` ` ` ` `// If number formed by last two ` ` ` `// digits is divisible by 4. ` ` ` `int` `last = str[n-1] - ` `'0'` `; ` ` ` `int` `second_last = str[n-2] - ` `'0'` `; ` ` ` ` ` `return` `((second_last*10 + last) ` ` ` `% 4 == 0); ` ` ` `} ` ` ` ` ` `// main function ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `String str = ` `"76952"` `; ` ` ` ` ` `if` `(check(str)) ` ` ` `Console.Write(` `"Yes"` `); ` ` ` `else` ` ` `Console.Write(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is Contributed by nitin mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find if a ` `// number is divisible by ` `// 4 or not ` ` ` `// Function to find that ` `// number divisible by ` `// 4 or not ` `function` `check(` `$str` `) ` `{ ` ` ` `$n` `= ` `strlen` `(` `$str` `); ` ` ` ` ` `// Empty string ` ` ` `if` `(` `$n` `== 0) ` ` ` `return` `false; ` ` ` ` ` `// If there is single digit ` ` ` `if` `(` `$n` `== 1) ` ` ` `return` `((` `$str` `[0] - ` `'0'` `) % 4 == 0); ` ` ` ` ` `// If number formed by ` ` ` `// last two digits is ` ` ` `// divisible by 4. ` ` ` `$last` `= ` `$str` `[` `$n` `- 1] - ` `'0'` `; ` ` ` `$second_last` `= ` `$str` `[` `$n` `- 2] - ` `'0'` `; ` ` ` `return` `((` `$second_last` `* 10 + ` `$last` `) % 4 == 0); ` `} ` ` ` `// Driver code ` `$str` `= ` `"76952"` `; ` `$x` `= check(` `$str` `)? ` `"Yes"` `: ` `"No"` `; ` `echo` `(` `$x` `); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

Output:

Yes

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.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Check if a large number is divisible by 9 or not
- Check if any large number is divisible by 19 or not
- Check if any large number is divisible by 17 or not
- To check whether a large number is divisible by 7
- Check if a large number is divisible by 5 or not
- Check whether a large number is divisible by 53 or not
- Check if a large number is divisible by 75 or not
- Check if a large number is divisible by 13 or not
- Check if a large number is divisible by 20
- Check if a large number is divisible by 6 or not
- Check if a large number is divisible by 2, 3 and 5 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 25 or not
- Check if a large number is divisible by 3 or not
- Check a large number is divisible by 16 or not
- Check if the large number formed is divisible by 41 or not
- Check if any permutation of a large number is divisible by 8
- Given a large number, check if a subsequence of digits is divisible by 8
- To check divisibility of any large number by 999