Write an Efficient Method to Check if a Number is Multiple of 3
Last Updated :
29 Mar, 2025
Given an integer n, the task is to determine whether n is a multiple of 3 or not.
Examples:
Input: n = 9
Output: Yes
Explanation: 9 is divisible by 3, so it is a multiple of 3.
Input: n = 14
Output: No
Explanation: 14 is not divisible by 3, so it is not a multiple of 3.
Input: n = 21
Output: Yes
Explanation: 21 is divisible by 3, so it is a multiple of 3.
[Approach 1] Using Repeated Sum of Digits – O(log(n)) Time and O(1) Space
The idea is based on the divisibility rule of 3, which states that a number is a multiple of 3 if the sum of its digits is also a multiple of 3. Instead of directly checking divisibility, we repeatedly sum the digits until we get a single-digit number. If the final result is 3, 6, or 9, the number is divisible by 3; otherwise, it is not.
C++
// C++ program to check if a number is a
// multiple of 3 using Repeated Sum of Digits
#include <bits/stdc++.h>
using namespace std;
// Function to compute sum of digits
int sumOfDigits(int n) {
int sum = 0;
while (n > 0) {
// Add last digit to sum
sum += n % 10;
// Remove last digit
n /= 10;
}
return sum;
}
// Function to check if n is a multiple of 3
bool isMultipleOf3(int n) {
// Make n positive
if (n < 0) {
n = -n;
}
while (n > 9) {
// Replace n with sum of its digits
n = sumOfDigits(n);
}
// Check if final result is 3, 6, or 9
return (n == 3 || n == 6 || n == 9);
}
int main() {
int n = 9;
if (isMultipleOf3(n)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
Java
// Java program to check if a number is a
// multiple of 3 using Repeated Sum of Digits
class GfG {
// Function to compute sum of digits
static int sumOfDigits(int n) {
int sum = 0;
while (n > 0) {
// Add last digit to sum
sum += n % 10;
// Remove last digit
n /= 10;
}
return sum;
}
// Function to check if n is a multiple of 3
static boolean isMultipleOf3(int n) {
// Make n positive
if (n < 0) {
n = -n;
}
while (n > 9) {
// Replace n with sum of its digits
n = sumOfDigits(n);
}
// Check if final result is 3, 6, or 9
return (n == 3 || n == 6 || n == 9);
}
public static void main(String[] args) {
int n = 9;
if (isMultipleOf3(n)) {
System.out.println("Yes");
}
else {
System.out.println("No");
}
}
}
Python
# Python program to check if a number is a
# multiple of 3 using Repeated Sum of Digits
# Function to compute sum of digits
def sumOfDigits(n):
sum = 0
while n > 0:
# Add last digit to sum
sum += n % 10
# Remove last digit
n //= 10
return sum
# Function to check if n is a multiple of 3
def isMultipleOf3(n):
# Make n positive
if n < 0:
n = -n
while n > 9:
# Replace n with sum of its digits
n = sumOfDigits(n)
# Check if final result is 3, 6, or 9
return (n == 3 or n == 6 or n == 9)
if __name__ == "__main__":
n = 9
if isMultipleOf3(n):
print("Yes")
else:
print("No")
C#
// C# program to check if a number is a
// multiple of 3 using Repeated Sum of Digits
using System;
class GfG {
// Function to compute sum of digits
static int sumOfDigits(int n) {
int sum = 0;
while (n > 0) {
// Add last digit to sum
sum += n % 10;
// Remove last digit
n /= 10;
}
return sum;
}
// Function to check if n is a multiple of 3
static bool isMultipleOf3(int n) {
// Make n positive
if (n < 0) {
n = -n;
}
while (n > 9) {
// Replace n with sum of its digits
n = sumOfDigits(n);
}
// Check if final result is 3, 6, or 9
return (n == 3 || n == 6 || n == 9);
}
static void Main() {
int n = 9;
if (isMultipleOf3(n)) {
Console.WriteLine("Yes");
}
else {
Console.WriteLine("No");
}
}
}
JavaScript
// JavaScript program to check if a number is a
// multiple of 3 using Repeated Sum of Digits
// Function to compute sum of digits
function sumOfDigits(n) {
let sum = 0;
while (n > 0) {
// Add last digit to sum
sum += n % 10;
// Remove last digit
n = Math.floor(n / 10);
}
return sum;
}
// Function to check if n is a multiple of 3
function isMultipleOf3(n) {
// Make n positive
if (n < 0) {
n = -n;
}
while (n > 9) {
// Replace n with sum of its digits
n = sumOfDigits(n);
}
// Check if final result is 3, 6, or 9
return (n == 3 || n == 6 || n == 9);
}
let n = 9;
if (isMultipleOf3(n)) {
console.log("Yes");
}
else {
console.log("No");
}
[Approach 2] Using Binary Representation – O(log(n)) Time and O(log(n)) Space
The idea is based on the binary representation of a number. The observation is that even powers of 2 contribute (3k+1) and odd powers contribute (3k-1) modulo 3. By counting set bits at odd and even positions and checking their difference, we determine if the number is divisible by 3. The approach uses recursion, reducing n by half in each step.
Mathematical Proof:
Consider a number n represented in binary form as abcde. Its decimal equivalent is:
n = (2^4 * a) + (2^3 * b) + (2^2 * c) + (2^1 * d) + (2^0 * e)
Observing powers of 2 modulo 3, we derive:
- Even powers of 2 can be expressed as 3k + 1 (e.g., 2^0 = 1, 2^2 = 3k + 1).
- Odd powers of 2 can be expressed as 3k – 1 (e.g., 2^1 = 3k – 1, 2^3 = 3k – 1).
Rewriting the decimal representation in terms of multiples of 3, we get:
n = (3k+1) * a + (3k-1) * b + (3k+1) * c + (3k-1) * d + (3k+1) * e
Rearranging the terms:
n = (3k)(a+b+c+d+e) + (a + c + e) – (b + d)
Since the first term is always a multiple of 3, for n to be a multiple of 3, the expression (a + c + e) – (b + d) must also be a multiple of 3. Thus, a number is divisible by 3 if and only if the difference between the count of set bits at odd positions and even positions is a multiple of 3.
C++
// C++ program to check if a number is a
// multiple of 3 using Binary
// Representation
#include <bits/stdc++.h>
using namespace std;
// Function to check if n is a multiple of 3
bool isMultipleOf3(int n) {
// Make n positive
if (n < 0) {
n = -n;
}
// Base cases
if (n == 0) {
return true;
}
if (n == 1) {
return false;
}
int odd_count = 0, even_count = 0;
while (n) {
// If rightmost bit is set, increment odd_count
if (n & 1) {
odd_count++;
}
// Right shift n
n >>= 1;
// If rightmost bit is set, increment even_count
if (n & 1) {
even_count++;
}
// Right shift n again
n >>= 1;
}
// Recursive call with the difference
return isMultipleOf3(abs(odd_count - even_count));
}
int main() {
int n = 9;
if (isMultipleOf3(n)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
Java
// Java program to check if a number is a
// multiple of 3 using Binary
// Representation
class GfG {
// Function to check if n is a multiple of 3
static boolean isMultipleOf3(int n) {
// Make n positive
if (n < 0) {
n = -n;
}
// Base cases
if (n == 0) {
return true;
}
if (n == 1) {
return false;
}
int odd_count = 0, even_count = 0;
while (n > 0) {
// If rightmost bit is set, increment odd_count
if ((n & 1) == 1) {
odd_count++;
}
// Right shift n
n >>= 1;
// If rightmost bit is set, increment even_count
if ((n & 1) == 1) {
even_count++;
}
// Right shift n again
n >>= 1;
}
// Recursive call with the difference
return isMultipleOf3(Math.abs(odd_count - even_count));
}
public static void main(String[] args) {
int n = 9;
if (isMultipleOf3(n)) {
System.out.println("Yes");
}
else {
System.out.println("No");
}
}
}
Python
# Python program to check if a number is a
# multiple of 3 using Binary
# Representation
# Function to check if n is a multiple of 3
def isMultipleOf3(n):
# Make n positive
if n < 0:
n = -n
# Base cases
if n == 0:
return True
if n == 1:
return False
odd_count = 0
even_count = 0
while n:
# If rightmost bit is set, increment odd_count
if n & 1:
odd_count += 1
# Right shift n
n >>= 1
# If rightmost bit is set, increment even_count
if n & 1:
even_count += 1
# Right shift n again
n >>= 1
# Recursive call with the difference
return isMultipleOf3(abs(odd_count - even_count))
if __name__ == "__main__":
n = 9
if isMultipleOf3(n):
print("Yes")
else:
print("No")
C#
// C# program to check if a number is a
// multiple of 3 using Binary
// Representation
using System;
class GfG {
// Function to check if n is a multiple of 3
static bool isMultipleOf3(int n) {
// Make n positive
if (n < 0) {
n = -n;
}
// Base cases
if (n == 0) {
return true;
}
if (n == 1) {
return false;
}
int odd_count = 0, even_count = 0;
while (n > 0) {
// If rightmost bit is set, increment odd_count
if ((n & 1) == 1) {
odd_count++;
}
// Right shift n
n >>= 1;
// If rightmost bit is set, increment even_count
if ((n & 1) == 1) {
even_count++;
}
// Right shift n again
n >>= 1;
}
// Recursive call with the difference
return isMultipleOf3(Math.Abs(odd_count - even_count));
}
public static void Main() {
int n = 9;
if (isMultipleOf3(n)) {
Console.WriteLine("Yes");
}
else {
Console.WriteLine("No");
}
}
}
JavaScript
// JavaScript program to check if a number is a
// multiple of 3 using Binary
// Representation
// Function to check if n is a multiple of 3
function isMultipleOf3(n) {
// Make n positive
if (n < 0) {
n = -n;
}
// Base cases
if (n === 0) {
return true;
}
if (n === 1) {
return false;
}
let odd_count = 0, even_count = 0;
while (n > 0) {
// If rightmost bit is set, increment odd_count
if (n & 1) {
odd_count++;
}
// Right shift n
n >>= 1;
// If rightmost bit is set, increment even_count
if (n & 1) {
even_count++;
}
// Right shift n again
n >>= 1;
}
// Recursive call with the difference
return isMultipleOf3(Math.abs(odd_count - even_count));
}
let n = 9;
if (isMultipleOf3(n)) {
console.log("Yes");
}
else {
console.log("No");
}
[Approach 3] Using Modulo Division – O(1) Time and O(1) Space
The idea is to use the modulo operation to check divisibility by 3 efficiently. A number n is a multiple of 3 if n % 3 == 0, meaning it leaves no remainder when divided by 3.
C++
// C++ program to check if a number is a
// multiple of 3 using Modulo Division
#include <bits/stdc++.h>
using namespace std;
// Function to check if n is a multiple of 3
bool isMultipleOf3(int n) {
return (n % 3 == 0);
}
int main() {
int n = 9;
if (isMultipleOf3(n)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
Java
// Java program to check if a number is a
// multiple of 3 using Modulo Division
class GfG {
// Function to check if n is a multiple of 3
static boolean isMultipleOf3(int n) {
return (n % 3 == 0);
}
public static void main(String[] args) {
int n = 9;
if (isMultipleOf3(n)) {
System.out.println("Yes");
}
else {
System.out.println("No");
}
}
}
Python
# Python program to check if a number is a
# multiple of 3 using Modulo Division
# Function to check if n is a multiple of 3
def isMultipleOf3(n):
return (n % 3 == 0)
if __name__ == "__main__":
n = 9
if isMultipleOf3(n):
print("Yes")
else:
print("No")
C#
// C# program to check if a number is a
// multiple of 3 using Modulo Division
using System;
class GfG {
// Function to check if n is a multiple of 3
static bool isMultipleOf3(int n) {
return (n % 3 == 0);
}
static void Main() {
int n = 9;
if (isMultipleOf3(n)) {
Console.WriteLine("Yes");
}
else {
Console.WriteLine("No");
}
}
}
JavaScript
// JavaScript program to check if a number is a
// multiple of 3 using Modulo Division
// Function to check if n is a multiple of 3
function isMultipleOf3(n) {
return (n % 3 === 0);
}
// Driver Code
let n = 9;
if (isMultipleOf3(n)) {
console.log("Yes");
}
else {
console.log("No");
}
Similar Reads
An efficient way to check whether n-th Fibonacci number is multiple of 10
We are given a variable n, we need to find whether Fibonacci number will be a multiple of 10 or not. Examples: Input : 15Output : Yes Input : 17Output : No A Simple Method is to find the nth Fibonacci number and check if it is divisible by 10 or not. C/C++ Code // A simple C++ program to check if //
7 min read
Efficiently check whether n is a multiple of 4 or not
Given a number n. The problem is to efficiently check whether n is a multiple of 4 or not without using arithmetic operators. Examples: Input : 16 Output : Yes Input : 14 Output : No Approach: A multiple of 4 always has 00 as its last two digits in its binary representation. We have to check whether
4 min read
Check whether a very large number of the given form is a multiple of 3.
Consider a very long K-digit number N with digits d0, d1, ..., dK-1 (in decimal notation; d0 is the most significant and dK-1 the least significant digit). This number is so large that it can't be given or written down explicitly; instead, only its starting digits are given and a way to construct th
13 min read
Check if a number is multiple of 5 without using / and % operators
Given a positive number n, write a function isMultipleof5(int n) that returns true if n is multiple of 5, otherwise false. You are not allowed to use % and / operators. Method 1 (Repeatedly subtract 5 from n) Run a loop and subtract 5 from n in the loop while n is greater than 0. After the loop term
13 min read
Check if a number is multiple of 9 using bitwise operators
Given a number n, write a function that returns true if n is divisible by 9, else false. The most simple way to check for n's divisibility by 9 is to do n%9. Another method is to sum the digits of n. If sum of digits is multiple of 9, then n is multiple of 9. The above methods are not bitwise operat
6 min read
Check if a number is power of k using base changing method
This program checks whether a number n can be expressed as power of k and if yes, then to what power should k be raised to make it n. Following example will clarify : Examples: Input : n = 16, k = 2 Output : yes : 4 Explanation : Answer is yes because 16 can be expressed as power of 2. Input : n = 2
9 min read
Check if LCM of array elements is divisible by a prime number or not
Given an array and a number k, the task is to find if LCM of the array is divisible by k or not.Examples : Input : int[] a = {10, 20, 15, 25} k = 3 Output : true Input : int[] a = {24, 21, 45, 57, 36}; k = 23; Output : false One simple solution is to first find LCM of array elements, then check if L
5 min read
Check if a number can be written as sum of three consecutive integers
Given an integer n, the task is to find whether n can be written as sum of three consecutive integer. If yes, find the three consecutive integer, else print "-1".Examples: Input: n = 6Output: 1 2 3Explanation: 6 = 1 + 2 + 3. Input: n = 7Output: -1 Recommended PracticeCheck for three consecutive numb
10 min read
Check if a number is divisible by 31 or not
Given a number N, the task is to check whether the number is divisible by 31 or not. Examples: Input: N = 1922 Output: Yes Explanation: 31 * 62 = 1922Input: N = 2722400 Output: No Approach: The divisibility test of 31 is: Extract the last digit.Subtract 3 * last digit from the remaining number obtai
5 min read
Check whether a given number is an ugly number or not
Given an integer N, the task is to find out whether the given number is an Ugly number or not . Ugly numbers are numbers whose only prime factors are 2, 3 or 5. Examples: Input: N = 14 Output: No Explanation: 14 is not ugly since it includes another prime factor 7. Input: N = 6 Output: Yes Explanati
9 min read