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

Given a number, the task is to find if the number is divisible by 9 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 = 69354 Output : Yes Input : n = 234567876799333 Output : No Input : n = 3635883959606670431112222 Output : No

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

A number is divisible by 9 if sum of its digits is divisible by 9.

**Illustration:**

For example n = 9432 Sum of digits = 9 + 4 + 3 + 2 = 18 Since sum is divisible by 9, answer is Yes.

**How does this work?**

Let us consider 1332, we can write it as 1332 = 1*1000 + 3*100 + 3*10 + 2 The proof is based on below observation: Remainder of 10^{i}divided by 9 is 1 So powers of 10 only results in remainder 1 when divided by 9. Remainder of "1*1000 + 3*100 + 3*10 + 2" divided by 9 can be written as : 1*1 + 3*1 + 3*1 + 2 = 9 The above expression is basically sum of all digits. Since 9 is divisible by 9, answer is yes.

Below is the implementation of above idea.

## C++

`// C++ program to find if a number is divisible by ` `// 9 or not ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find that number divisble by 9 or not ` `int` `check(string str) ` `{ ` ` ` `// Compute sum of digits ` ` ` `int` `n = str.length(); ` ` ` `int` `digitSum = 0; ` ` ` `for` `(` `int` `i=0; i<n; i++) ` ` ` `digitSum += (str[i]-` `'0'` `); ` ` ` ` ` `// Check if sum of digits is divisible by 9. ` ` ` `return` `(digitSum % 9 == 0); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string str = ` `"99333"` `; ` ` ` `check(str)? cout << ` `"Yes"` `: cout << ` `"No "` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find if a number is ` `// divisible by 9 or not ` `class` `IsDivisible ` `{ ` ` ` `// Function to find that number ` ` ` `// is divisble by 9 or not ` ` ` `static` `boolean` `check(String str) ` ` ` `{ ` ` ` `// Compute sum of digits ` ` ` `int` `n = str.length(); ` ` ` `int` `digitSum = ` `0` `; ` ` ` `for` `(` `int` `i=` `0` `; i<n; i++) ` ` ` `digitSum += (str.charAt(i)-` `'0'` `); ` ` ` ` ` `// Check if sum of digits is divisible by 9. ` ` ` `return` `(digitSum % ` `9` `== ` `0` `); ` ` ` `} ` ` ` ` ` `// main function ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `String str = ` `"99333"` `; ` ` ` `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 ` `# 9 or not ` ` ` `# Function to find that ` `# number divisble by 9 ` `# or not ` `def` `check(st) : ` ` ` ` ` `# Compute sum of digits ` ` ` `n ` `=` `len` `(st) ` ` ` `digitSum ` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(` `0` `,n) : ` ` ` `digitSum ` `=` `digitSum ` `+` `(` `int` `)(st[i]) ` ` ` ` ` `# Check if sum of digits ` ` ` `# is divisible by 9. ` ` ` `return` `(digitSum ` `%` `9` `=` `=` `0` `) ` ` ` `# Driver code ` `st ` `=` `"99333"` ` ` `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 9 or not. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to find that number ` ` ` `// is divisble by 9 or not ` ` ` `static` `bool` `check(String str) ` ` ` `{ ` ` ` ` ` `// Compute sum of digits ` ` ` `int` `n = str.Length; ` ` ` `int` `digitSum = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `digitSum += (str[i] - ` `'0'` `); ` ` ` ` ` `// Check if sum of digits is ` ` ` `// divisible by 9. ` ` ` `return` `(digitSum % 9 == 0); ` ` ` `} ` ` ` ` ` `// main function ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `String str = ` `"99333"` `; ` ` ` `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 9 or not ` ` ` `// Function to find that ` `// number divisble by 9 or not ` `function` `check(` `$str` `) ` `{ ` ` ` ` ` `// Compute sum of digits ` ` ` `$n` `= ` `strlen` `(` `$str` `); ` ` ` `$digitSum` `= 0; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `$digitSum` `+= (` `$str` `[` `$i` `] - ` `'0'` `); ` ` ` ` ` `// Check if sum of digits ` ` ` `// is divisible by 9. ` ` ` `return` `(` `$digitSum` `% 9 == 0); ` `} ` ` ` `// Driver code ` `$str` `= ` `"99333"` `; ` `$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.

## Recommended Posts:

- Check if a large number is divisible by 25 or not
- To check whether a large number is divisible by 7
- Check a large number is divisible by 16 or not
- Check if a large number is divisible by 75 or not
- Check if a large number is divisible by 20
- Check if a large number is divisible by 2, 3 and 5 or not
- 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 4 or not
- Check if any large number is divisible by 17 or not
- Check if any large number is divisible by 19 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 13 or not
- Check if a large number is divisible by 5 or not