Related Articles
Check if a large number is divisible by 2, 3 and 5 or not
• Last Updated : 13 Nov, 2018

Given a number, the task is to check if a number is divisible by 2, 3 and 5 or not. The input number may be large and it may not be possible to store even if we use long long int so the number is taken as a string.

Examples:

```Input : str = "725"
Output : NO

Input : str = "263730746028908374890"
Output : YES
```

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

A number is divisible by 2 if it’s right most digit is even and also a number is divisible by 5 if it’s right most digit is zero or five.

So, from above two observations, one can conclude that for the number to be divisible by both 2 and 5 the rightmost digit of the number must be zero.

Now, a number is divisible by 3 if the sum of its digits is divisible by three.

Therefore, a number will be divisible by all of 2, 3 and 5 if:

• Its rightmost digit is zero.
• Sum of all of its digits is divisible by 3.

Below is the implementation of the above approach:

## C++

 `// CPP program to Check if a large number is``// divisible by 2, 3 and 5 or not.``#include ``using` `namespace` `std;`` ` `// function to return sum of digits of``// a number``int` `SumOfDigits(string str, ``int` `n)``{``    ``int` `sum = 0;`` ` `    ``for` `(``int` `i = 0; i < n; i++)``        ``sum += (``int``)(str[i] - ``'0'``);`` ` `    ``return` `sum;``}`` ` `// function to Check if a large number is``// divisible by 2, 3 and 5 or not``bool` `Divisible(string str, ``int` `n)``{``    ``if` `(SumOfDigits(str, n) % 3 == 0 and str[n - 1] == ``'0'``)``        ``return` `true``;`` ` `    ``return` `false``;``}`` ` `// Driver code``int` `main()``{``    ``string str = ``"263730746028908374890"``;`` ` `    ``int` `n = str.size();`` ` `    ``if` `(Divisible(str, n))``        ``cout << ``"YES"``;``    ``else``        ``cout << ``"NO"``;`` ` `    ``return` `0;``}`

## Java

 `// Java program to Check if a large``// number is divisible by 2, 3 and ``// 5 or not.``class` `GFG``{``// function to return sum of ``// digits of a number``static` `int` `SumOfDigits(String str,``                       ``int` `n)``{``    ``int` `sum = ``0``;`` ` `    ``for` `(``int` `i = ``0``; i < n; i++)``        ``sum += (``int``)(str.charAt(i) - ``'0'``);`` ` `    ``return` `sum;``}`` ` `// function to Check if a large number ``// is divisible by 2, 3 and 5 or not``static` `boolean` `Divisible(String str,``                         ``int` `n)``{``    ``if` `(SumOfDigits(str, n) % ``3` `== ``0` `&& ``        ``str.charAt(n - ``1``) == ``'0'``)``        ``return` `true``;`` ` `    ``return` `false``;``}`` ` `// Driver code``public` `static` `void` `main(String []args)``{``    ``String str = ``"263730746028908374890"``;`` ` `    ``int` `n = str.length();`` ` `    ``if` `(Divisible(str, n))``        ``System.out.println(``"YES"``);``    ``else``        ``System.out.println(``"NO"``);``}``}`` ` `// This code is contributed by ihritik`

## Python 3

 `# Python 3 program to Check if ``# a large number is``# divisible by 2, 3 and 5 or not.`` ` `# function to return sum of digits of``# a number``def` `SumOfDigits(``str``, n):``     ` `    ``sum` `=` `0``    ``for` `i ``in` `range``(``0``,n):``        ``sum` `+``=` `int``(``ord``(``str``[i] )``-` `ord``(``'0'``))`` ` `    ``return` `sum`` ` `# function to Check if a large number is``# divisible by 2, 3 and 5 or not``def` `Divisible(``str``, n):``    ``if` `((SumOfDigits(``str``, n) ``%` `3` `=``=` `0` `and` `        ``str``[n ``-` `1``] ``=``=` `'0'``)):``        ``return` `True`` ` `    ``return` `False`` ` `# Driver code``if` `__name__ ``=``=` `"__main__"``:``    ``str` `=` `"263730746028908374890"`` ` `    ``n ``=` `len``(``str``)`` ` `    ``if` `(Divisible(``str``, n)):``        ``print``(``"YES"``)``    ``else``:``        ``print``(``"NO"``)``         ` `# this code is contributed by``# ChitraNayal`

## C#

 `// C# program to Check if a large number ``// is divisible by 2, 3 and 5 or not.``using` `System;`` ` `class` `GFG``{``// function to return sum of digits ``// of a number``static` `int` `SumOfDigits(String str,``                       ``int` `n)``{``    ``int` `sum = 0;`` ` `    ``for` `(``int` `i = 0; i < n; i++)``        ``sum += (``int``)(str[i] - ``'0'``);`` ` `    ``return` `sum;``}`` ` `// function to Check if a large number ``// is divisible by 2, 3 and 5 or not``static` `bool` `Divisible(String str, ``int` `n)``{``    ``if` `(SumOfDigits(str, n) % 3 == 0 && ``                    ``str[n - 1] == ``'0'``)``        ``return` `true``;`` ` `    ``return` `false``;``}`` ` `// Driver code``public` `static` `void` `Main()``{``    ``String str = ``"263730746028908374890"``;`` ` `    ``int` `n = str.Length;`` ` `    ``if` `(Divisible(str, n))``        ``Console.WriteLine(``"YES"``);``    ``else``        ``Console.WriteLine(``"NO"``);``}``}`` ` `// This code is contributed by ihritik`

## PHP

 ``
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.  Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA

My Personal Notes arrow_drop_up