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

28 Jan, 2022
Given a number, the task is that we divide number by 3. The input number may be large and it may not be possible to store even if we use long long int.Examples:  
Input  : n = 769452
Output : Yes

Input  : n = 123456758933312
Output : No

Input  : n = 3635883959606670431112222
Output : Yes
 
Since input number may be very large, we cannot use n % 3 to check if a number is divisible by 3 or not, especially in languages like C/C++. The idea is based on following fact. 
A number is divisible by 3 if sum of its digits is divisible by 3.
Illustration:  
For example n = 1332
Sum of digits = 1 + 3 + 3 + 2
             = 9
Since sum is divisible by 3,
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 10i divided by 3 is 1
So powers of 10 only result in value 1.

Remainder of "1*1000 + 3*100 + 3*10 + 2"
divided by 3 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 3, answer is yes.
Below is the implementation of above fact : 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find if a number is divisible by// 3 or not#include<bits/stdc++.h>using namespace std; // Function to find that number divisible by 3 or notint 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 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 3 or notclass IsDivisible{    // Function to find that number    // divisible by 3 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 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 program to find if a number is# divisible by 3 or not # Function to find that number# divisible by 3 or notdef check(num) :         # Compute sum of digits    digitSum = 0    while num > 0 :        rem = num % 10        digitSum = digitSum + rem        num = num / 10             # Check if sum of digits is    # divisible by 3.    return (digitSum % 3 == 0)     # main functionnum = 1332if(check(num)) :    print ("Yes")else :    print ("No")     # This code is contributed by Nikita Tiwari.
// C# program to find if a number is// divisible by 3 or notusing System; class GFG{    // Function to find that number    // divisible by 3 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 3.        return (digitSum % 3 == 0);    }     // main function    public static void Main ()    {        string str = "1332";                 if(check(str))            Console.WriteLine("Yes");        else            Console.WriteLine("No");    }} // This code is contributed by vt_m.
<?php// PHP program to find if a// number is divisible by// 3 or not // Function to find that// number divisible by 3 or notfunction 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 3.    return ($digitSum % 3 == 0);} // Driver code$str = "1332";$x = check($str) ? "Yes" : "No ";echo($x); // This code is contributed by Ajit.?>
<script>// Javascript program to find if a// number is divisible by// 3 or not // Function to find that// number divisible by 3 or notfunction check(str){         // Compute sum of digits    let n = str.length;    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 codelet str = "1332";let x = check(str) ? "Yes" : "No ";document.write(x); // This code is contributed by _saurabh_jaiswal.</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 review-team@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. 
jit_t
ManasChhabra2
_saurabh_jaiswal
amartyaghoshgfg
divisibility
large-numbers
number-digits
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find GCD or HCF of two numbers
Sieve of Eratosthenes
Print all possible combinations of r elements in a given array of size n
Operators in C / C++
Program for factorial of a number
The Knight's tour problem | Backtracking-1
Find minimum number of coins that make a given value