Check if a large number is divisible by 6 or not - GeeksforGeeks

13 May, 2021
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
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++
Java
Python3
C#
PHP
Javascript
// C++ program to find if a number is divisible by// 6 or not#include<bits/stdc++.h>using namespace std; // Function to find that number divisible by 6 or notbool 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);} // Driver codeint main(){    string str = "1332";    check(str)?  cout << "Yes" : cout << "No ";    return 0;}
// Java program to find if a number is// divisible by 6 or notclass 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<n; i++)            digitSum += (str.charAt(i)-'0');              // Check if sum of digits is divisible by 3        return (digitSum % 3 == 0);    }         // main function    public static void main (String[] args)    {        String str = "1332";        if(check(str))            System.out.println("Yes");        else            System.out.println("No");    }}
# Python 3 program to find# if a number is divisible# by 6 or not # Function to find that number# is divisible by 6 or notdef 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 codest = "1332"if(check(st)) :    print("Yes")else :    print("No ")     # This article is contributed by Nikita Tiwari.
// C# program to find if a number is// divisible by 6 or notusing 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// PHP program to find if a// number is divisible by// 6 or not // Function to find that number// divisible by 6 or notfunction check($str){    $n = strlen($str);     // 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    $digitSum = 0;    for ($i = 0; $i < $n; $i++)        $digitSum += ($str[$i] - '0');     // Check if sum of digits    // is divisible by 3    return ($digitSum % 3 == 0);}     // Driver code    $str = "1332";    if(check($str))        echo "Yes" ;    else        echo " No ";    return 0; // This code is contributed by nitin mittal.?>
<script> // JavaScript program for the above approach     // Function to find that number    // divisible by 6 or not    function check(str)    {        let 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        let digitSum = 0;        for (let i = 0; i < n; i++)            digitSum += (str[i] - '0');               // Check if sum of digits is        // divisible by 3        return (digitSum % 3 == 0);    } // Driver Code     let str = "1332";     if(check(str))        document.write("Yes");     else        document.write("No"); // This code is contributed by splevel62.</script>
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 write.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. 
parashar
nitin mittal
ManasChhabra2
splevel62
CoderSaty
divisibility
large-numbers
number-digits
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Count all possible paths from top left to bottom right of a mXn matrix
Modular multiplicative inverse
Program to multiply two matrices
Fizz Buzz Implementation
Check if a number is Palindrome
Count ways to reach the n'th stair
Merge two sorted arrays with O(1) extra space
Min Cost Path | DP-6