Open in App
Not now

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

• Difficulty Level : Basic
• Last Updated : 29 Mar, 2023

Given a number, the task is to check if a number is divisible by 6 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 = 2112
Output: Yes

Input : n = 1124
Output : No

Input  : n = 363588395960667043875487
Output : No```

## C++

 `#include ` `using` `namespace` `std;`   `int` `main() {`   `    ``long` `n = 36358839596;` `  `  `      ``// finding given number is divisible by 6 or not` `    ``if` `(n % 6 == 0)` `        ``cout << ``"Yes"``;` `    ``else` `        ``cout << ``"No"``;` `    ``return` `0;` `}`   `// This code is contributed by lokesh`

## Java

 `// Java code for the above approach` `// To check whether the given number is divisible by 6 or not` `import` `java.io.*;`   `class` `GFG {` `    ``public` `static` `void` `main (String[] args) {` `        ``long` `n = 36358839596L;` ` `  `        ``// finding given number is divisible by 6 or not` `        ``if` `(n % ``6` `== ``0``)` `              ``System.out.print(``"Yes"``);` `        ``else` `              ``System.out.print(``"No"``);` `    ``}` `}`   `// This code is contributed by lokesh`

## Python3

 `# Python code ` `# To check whether the given number is divisible by 6 or not`   `#input ` `n``=``363588395960667043875487` `# the above input can also be given as n=input() -> taking input from user` `# finding given number is divisible by 6 or not` `if` `int``(n)``%``6``=``=``0``:` `  ``print``(``"Yes"``) ` `else``: ` `  ``print``(``"No"``)`

## C#

 `using` `System;` `public` `class` `GFG {`   `  ``static` `public` `void` `Main()` `  ``{`   `    ``// C# code for the above approach` `    ``// To check whether the given number is divisible by 6 or not`   `    ``//input` `    ``long` `n = 36358839596;`   `    ``// finding given number is divisible by 6 or not` `    ``if` `(n % 6 == 0)` `      ``Console.Write(``"Yes"``);` `    ``else` `      ``Console.Write(``"No"``);`   `  ``} ` `}`   `// This code is contributed by ksrikanth0498`

## Javascript

 ``

## PHP

 ``

Output

`No`

Time complexity: O(1) as it is performing constant operations
Auxiliary Space: O(1) as it is using constant space for variables

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

```A number is divisible by 6 it's divisible by 2 and 3.
a)  A number is divisible by 2 if its last digit is divisible by 2.
b)  A number is divisible by 3 if sum of digits is divisible by 3.```

Below is the implementation based on above steps.

## C++

 `// C++ program to find if a number is divisible by` `// 6 or not` `#include` `using` `namespace` `std;`   `// Function to find that number divisible by 6 or not` `bool` `check(string str)` `{` `    ``int` `n = str.length();`   `    ``// Return false if number is not divisible by 2.` `    ``if` `((str[n-1]-``'0'``)%2 != 0)` `       ``return` `false``;`   `    ``// If we reach here, number is divisible by 2.` `    ``// Now check for 3.`   `    ``// Compute sum of digits` `    ``int` `digitSum = 0;` `    ``for` `(``int` `i=0; i

## Java

 `// Java program to find if a number is` `// divisible by 6 or not` `import` `java.io.*;` `class` `IsDivisible` `{` `    ``// Function to find that number divisible by 6 or not` `    ``static` `boolean` `check(String str)` `    ``{` `        ``int` `n = str.length();` `     `  `        ``// Return false if number is not divisible by 2.` `        ``if` `((str.charAt(n-``1``) -``'0'``)%``2` `!= ``0``)` `           ``return` `false``;` `     `  `        ``// If we reach here, number is divisible by 2.` `        ``// Now check for 3.` `     `  `        ``// Compute sum of digits` `        ``int` `digitSum = ``0``;` `        ``for` `(``int` `i=``0``; i

## Python3

 `# Python 3 program to find ` `# if a number is divisible` `# by 6 or not`   `# Function to find that number ` `# is divisible by 6 or not` `def` `check(st) :` `    ``n ``=` `len``(st)` `    `  `    `  `    ``# Return false if number` `    ``# is not divisible by 2.` `    ``if` `(((``int``)(st[n``-``1``])``%``2``) !``=` `0``) :` `        ``return` `False` ` `  `    ``# If we reach here, number ` `    ``# is divisible by 2. Now` `    ``# check for 3.` ` `  `    ``# Compute sum of digits` `    ``digitSum ``=` `0` `    ``for` `i ``in` `range``(``0``, n) :` `        ``digitSum ``=` `digitSum ``+` `(``int``)(st[i])` ` `  `    ``# Check if sum of digits` `    ``# is divisible by 3` `    ``return` `(digitSum ``%` `3` `=``=` `0``)`     `# Driver code` `st ``=` `"1332"` `if``(check(st)) :` `    ``print``(``"Yes"``)` `else` `:` `    ``print``(``"No "``)` `    `  `# This article is contributed by Nikita Tiwari.`

## C#

 `// C# program to find if a number is` `// divisible by 6 or not` `using` `System;`   `class` `GFG {` `    `  `    ``// Function to find that number` `    ``// divisible by 6 or not` `    ``static` `bool` `check(String str)` `    ``{` `        ``int` `n = str.Length;` `    `  `        ``// Return false if number is` `        ``// not divisible by 2.` `        ``if` `((str[n-1] -``'0'``) % 2 != 0)` `            ``return` `false``;` `    `  `        ``// If we reach here, number is` `        ``// divisible by 2.` `        ``// Now check for 3.` `    `  `        ``// Compute sum of digits` `        ``int` `digitSum = 0;` `        ``for` `(``int` `i = 0; i < n; i++)` `            ``digitSum += (str[i] - ``'0'``);` `    `  `        ``// Check if sum of digits is` `        ``// divisible by 3` `        ``return` `(digitSum % 3 == 0);` `    ``}` `    `  `    ``// main function` `    ``public` `static` `void` `Main () ` `    ``{` `        ``String str = ``"1332"``;` `        `  `        ``if``(check(str))` `            ``Console.Write(``"Yes"``);` `        ``else` `            ``Console.Write(``"No"``);` `    ``}` `}`   `// This code is contributed by parashar.`

## PHP

 ``

## Javascript

 ``

Output

`Yes`

Time Complexity: O(logN) where N is the given number
Auxiliary Space: O(1) since no extra space is being used

This article is contributed by DANISH_RAZA . If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

#### Approach#3: using the fact that it is divisible by 3 and 2 consecutive even numbers

We can check if a number is divisible by 6 by checking if it is divisible by 3 and any 2 consecutive even numbers (i.e., 2 and 4 or 4 and 6, etc.). We can use the modulo operation to find the remainder of the number when divided by 3 and check if it is divisible by any 2 consecutive even numbers.

#### Algorithm

1. Check if the remainder of the number when divided by 3 is 0.
2. Check if the last digit of the number is even.
3. If the last digit of the number is even, check if the second-last digit of the number is odd.
4. If the second-last digit of the number is odd, print “Yes”, otherwise print “No”.

## Python3

 `def` `check_divisibility_by_6(n):` `    ``if` `n ``%` `3` `=``=` `0` `and` `n ``%` `10` `%` `2` `=``=` `0` `and` `(n ``/``/` `10``) ``%` `10` `%` `2` `!``=` `0``:` `        ``print``(``"Yes"``)` `    ``else``:` `        ``print``(``"No"``)` `n``=``2112` `check_divisibility_by_6(n)`   `n``=``1124` `check_divisibility_by_6(n)`

Output

```Yes
No```

Time Complexity: O(log n) where n is the value of the number.

Auxiliary Space: O(1).

My Personal Notes arrow_drop_up
Related Articles