Before reading this code you should know a little bit about Javascript function, OR operator.
What is an OR operator? It will return false when all the output is false otherwise it will return true. Let’s see some examples.
true || false = true
true || true = true
false || true = true
false || false = false
We will use just the javascript function to write the code.
function divisible(num)
{
if(num % 3==0 || num % 7 == 0)
{
console.log(`The number ${num} is divisible by 3 or 7`)
}
else
{
console.log(`No, the number ${num} is not divisible by 3 or 7`)
}
}
divisible(3)
divisible(8)
divisible(88)
divisible(21)
The output of the code is
The number 3 is divisible by 3 or 7
No, the number 8 is not divisible by 3 or 7
No, the number 88 is not divisible by 3 or 7
The number 21 is divisible by 3 or 7
Write the above code by taking the input from the user.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Javascript</title>
</head>
<body>
<div>
<h3>Write a javascript program to check if a given positive number is a multiple of 3 or a multiple of 7</h3>
Enter the number:<input type="text" id="num1"><br />
<button onclick="divisible()">Submit</button>
</div>
<script>
function divisible()
{
var num = parseInt(document.getElementById("num1").value)
if(num % 3==0 || num % 7 == 0)
{
console.log(`The number ${num} is divisible by 3 or 7`)
}
else
{
console.log(`No, the number ${num} is not divisible by 3 or 7`)
}
}
</script>
</body>
</html>