JavaScript: Check whether a given positive number is a multiple of 3 or a multiple of 7
JavaScript Basic: Exercise-25 with Solution
Check if Number is Multiple of 3 or 7
Write a JavaScript program to check whether a given positive number is a multiple of 3 or 7.
This JavaScript program checks if a given positive number is a multiple of either 3 or 7. It uses the modulo operator (%) to determine if the number is divisible by 3 or 7 without any remainder. If the result is 0 for either operation, it means the number is a multiple of 3 or 7, respectively.
Visual Presentation:

Sample Solution:
JavaScript Code:
// Define a function named test37 that takes a parameter x
function test37(x) {
// Check if x is divisible by 3 or 7
if (x % 3 == 0 || x % 7 == 0) {
// If true, return true
return true;
}
// If not divisible by 3 or 7, return false
else {
return false;
}
}
// Log the result of calling the test37 function with the argument 12 to the console
console.log(test37(12));
// Log the result of calling the test37 function with the argument 14 to the console
console.log(test37(14));
// Log the result of calling the test37 function with the argument 10 to the console
console.log(test37(10));
// Log the result of calling the test37 function with the argument 11 to the console
console.log(test37(11));
Output:
true true false false
Live Demo:
See the Pen JavaScript: check if a given positive number is a multiple of 3 or 7.-basic-ex-25 by w3resource (@w3resource) on CodePen.
Flowchart:

ES6 Version:
// Using ES6 arrow function syntax to define the test37 function
const test37 = (x) => {
// Check if x is divisible by 3 or 7
if (x % 3 == 0 || x % 7 == 0) {
// If true, return true
return true;
}
// If not divisible by 3 or 7, return false
else {
return false;
}
};
// Log the result of calling the test37 function with the argument 12 to the console
console.log(test37(12));
// Log the result of calling the test37 function with the argument 14 to the console
console.log(test37(14));
// Log the result of calling the test37 function with the argument 10 to the console
console.log(test37(10));
// Log the result of calling the test37 function with the argument 11 to the console
console.log(test37(11));
For more Practice: Solve these Related Problems:
- Write a JavaScript program that checks if a given number is a multiple of 3 or 7, but not both.
- Write a JavaScript program that determines if any number in an array is exclusively a multiple of 3 or 7.
- Write a JavaScript program that verifies if a positive integer is a multiple of either 3 or 7, prioritizing the smaller multiple if both apply.
Go to:
PREV : Add First Character to Front and Back of String.
NEXT : Add Last 3 Characters to Front and Back of String.
Improve this sample solution and post your code through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.