Open In App
Related Articles
• Write an Interview Experience
• Mathematical Algorithms

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

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 = 769452Output : YesInput  : n = 123456758933312Output : NoInput  : n = 3635883959606670431112222Output : 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 = 1332Sum of digits = 1 + 3 + 3 + 2             = 9Since sum is divisible by 3,answer is Yes.`

How does this work?

`Let us consider 1332, we can write it as1332 = 1*1000 + 3*100 + 3*10 + 2The proof is based on below observation:Remainder of 10i divided by 3 is 1So 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 = 9The above expression is basically sum ofall digits.Since 9 is divisible by 3, answer is yes.`

Below is the implementation of the above fact :

## C++

 `// C++ program to find if a number is divisible by``// 3 or not``#include``using` `namespace` `std;`` ` `// Function to find that number divisible by 3 or not``int` `check(string str)``{``    ``// Compute sum of digits``    ``int` `n = str.length();``    ``int` `digitSum = 0;``    ``for` `(``int` `i=0; i

## Java

 `// Java program to find if a number is``// divisible by 3 or not``import` `java.io.*;``class` `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

## Python3

 `# Python program to find if a number is``# divisible by 3 or not`` ` `# Function to find that number ``# divisible by 3 or not``def` `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 function``num ``=` `1332``if``(check(num)) :``    ``print` `(``"Yes"``)``else` `:``    ``print` `(``"No"``)``     ` `# This code is contributed by Nikita Tiwari.`

## C#

 `// C# program to find if a number is``// divisible by 3 or not``using` `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.`

## Javascript

 ``

## PHP

 ``

Output

```Yes

```

Time Complexity: O(n), where n is the number of digits in the input string. This is because the for loop is used to sum up all the digits in the string, and the loop runs for n iterations.
Auxiliary Space: O(1), as we are not using any extra space.

Method 2: Checking given number is divisible by 3 or not by using the modulo division operator “%”.

## C++

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

## Java

 `/*package whatever //do not write package name here */``import` `java.io.*;`` ` `class` `GFG {``  ``public` `static` `void` `main(String[] args)``  ``{`` ` `    ``// input``    ``long` `n = ``769452``;`` ` `     ` `    ``// finding given number is``    ``// divisible by 3 or not``    ``if` `(n % ``3` `== ``0``) {``      ``System.out.println(``"Yes"``);``    ``}``    ``else` `{``      ``System.out.println(``"No"``);``    ``}``  ``}``}`` ` `// This code is contributed by laxmigangarajula03`

## Python3

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

## C#

 `using` `System;`` ` `public` `class` `GFG{`` ` `  ``static` `public` `void` `Main (){`` ` `    ``//input``    ``long` `n = 769452;`` ` `     ` `    ``// finding given number is divisible by 3 or not``    ``if` `(n % 3 == 0)``    ``{``      ``Console.Write(``"Yes"``);``    ``}``    ``else``    ``{``      ``Console.Write(``"No"``);``    ``}  `` ` `  ``}``}`` ` `// This code is contributed by laxmigangarajula03`

## Javascript

 ``

## PHP

 ``

Output

```Yes

```

Time Complexity: O(1) as it is doing constant operations
Auxiliary Space: O(1)