JavaScript check if a number is multiple of 3 without using modulo

Introduction :

In this post, I will show you three different ways to check if a number is divisible by 3 or not without using the modulo % operator. For each example, we will use one for loop to iterate from 0 to 30 and print out the numbers that are divisible by 3.

Method 1: With divide and multiply:

This method will use the following steps:

  • Divide the number by 3
  • Convert this value to an integer
  • Multiply the result by 3 and check if it is equal to the original number or not.

For example, if the number is 21:

  • The result of 21/3 is 7.
  • The integer value of 7 is 7.
  • If you multiply 7 by 3, it will be 21 i.e. equal to the original number. So, 21 is divisible by 3.

If the number is 32:

  • The result of 32/3 is 10.666666666666666.
  • The integer value of the result is 10.
  • If you multiply 10 by 3, it will be 30, which is not equal to the original number. So, 32 is not divisible by 3.

JavaScript Program:

The following program uses the above approach:

const isMultipleOfThree = (num) => {
  const div = parseInt(num / 3);

  return num === div * 3;
};

for (let i = 0; i <= 30; i++) {
  if (isMultipleOfThree(i)) {
    console.log(i);
  }
}

Download it on GitHub

Explanation :

  • The above program uses one for loop to iterate over the numbers from 0 to 30.
  • It calls the isMultipleOfThree method to check if the current value of i is divisible by 3 or not.
    • The parseInt method is used to get the integer value of the division num/3. This value is assigned to the variable div.
    • The isMultipleOfThree method returns true if the original number is equal to the multiplication of div and 3. Else, it returns false.
  • If the return value of isMultipleOfThree is true, it prints that value of i.

Output:

If you run this program, it will print all the multiples of 3 between 0 to 30:

0;
3;
6;
9;
12;
15;
18;
21;
24;
27;
30;

JavaScript program to find all multiple of three

Method 2: With a recursive function:

This is a recursive approach to check if a number is divisible by 3 or not. It will keep subtracting 3 from the original number until it becomes zero or less than zero. If it becomes zero, that number is divisible by 3. And if it is less than zero, it is not.

JavaScript Program:

The following program uses a recursive function to check if a number is divisible by 3 or not:

const isMultipleOfThree = (num) => {
  if (num === 0) return true;

  if (num < 0) return false;

  return isMultipleOfThree(num - 3);
};

for (let i = 0; i <= 30; i++) {
  if (isMultipleOfThree(i)) {
    console.log(i);
  }
}

Download it on GitHub

Explanation:

  • The isMultipleOfThree_is a recursive method.
  • This method subtracts 3 from the number argument and calls itself again recursively.
  • If at any point, the argument becomes zero, it returns true. If it becomes negative, it returns false.
  • It uses a for loop from 0 to 30 and prints out the multipliers of 3.

It will print the below output:

0;
3;
6;
9;
12;
15;
18;
21;
24;
27;
30;

JavaScript check multiple three recursive

Method 3: By finding the digit sum:

If a number is divisible by 3, the sum of its digits should be divisible by 3. For example, the sum of digits for 1236 is 1 + 2 + 3 + 6 = 12 = 1 + 2 = 3, which is divisible by 3. So, 1236 is also divisible by 3.

We will write one function to find out the sum of the digits of a number. The main function will call this function. If the sum is 3, 6, or 9, the number will be divisible by 3.

JavaScript Program:

const sumDigits = (num) => {
  let sum = 0;
  while (num) {
    sum += num % 10;
    num = Math.floor(num / 10);
    if (num == 0 && sum > 9) {
      num = sum;
      sum = 0;
    }
  }
  return sum;
};

const isMultipleOfThree = (num) => {
  let sumOfDigits = sumDigits(num);
  return (
    sumOfDigits === 0 ||
    sumOfDigits === 3 ||
    sumOfDigits === 6 ||
    sumOfDigits === 9
  );
};

for (let i = 0; i <= 100; i++) {
  if (isMultipleOfThree(i)) {
    console.log(i);
  }
}

Download it on GitHub

  • The sumDigits method finds the sum of the digits of a number. It will keep adding the digits until it becomes a single-digit number. For example, the sum of digits of 66 is 12. But, since 12 is a two-digit number, it will again find the sum of the digits of 12, which is 3.
  • The isMultipleOfThree method calls the sumDigits method to find the sum of digits of a number. If the return value is 0, 3, 6, or 9, it will be divisible by 3.
  • The for loop runs from 0 to 100 and for each number it calls the isMultipleOfThree method and prints the multipliers of 3.

Output:

This program will print the below output:

0
3
6
9
12
15
18
21
24
27
30
33
36
39
42
45
48
51
54
57
60
63
66
69
72
75
78
81
84
87
90
93
96
99

It prints the multipliers of 3 in between 0 to 100.

Similar tutorials :