Program to check if a number is a gapful number
A gapful number is a 3 digit number whose first and last element when together divides the whole number.
Video: https://youtu.be/hxr8X-Je4kM
Example:
1. 210
210 is not a gapful number as 210 is not divisible by 20
2. 192
192 is a gapful number as 192 is divisible by 12
Program:
gapfulNumbers.html
gapfulNumbers.js
Output:

Using NodeJS:
Video: https://youtu.be/hxr8X-Je4kM
Example:
1. 210
210 is not a gapful number as 210 is not divisible by 20
2. 192
192 is a gapful number as 192 is divisible by 12
Program:
gapfulNumbers.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="gapfulNumbers.js"></script>
<title>Gapful Numbers</title>
</head>
<body>
<p>Enter a number to check if it is a gapful number</p>
<input type="number" id="inputValue" />
<button onclick="checkGapful()" >Check</button>
<p id="outputValue"></p>
</body>
</html>
gapfulNumbers.js
function checkGapful() {
const inputValue = document.getElementById('inputValue').value.toString();
if (!isNaN(inputValue)) {
let div = inputValue[0] + inputValue[inputValue.length - 1];
if (parseInt(inputValue) % parseInt(div) == 0) {
return printInfo(inputValue, true);
}
printInfo(inputValue, false);
} else {
document.getElementById('outputValue').innerHTML = 'Enter valid integers';
}
}
function printInfo(inputValue, isGapful) {
document.getElementById('outputValue').innerHTML = inputValue + ' is ' + (isGapful ? '' : 'not') + ' a gapful number';
}
Output:

Using NodeJS:
process.stdin.setEncoding('utf-8');
console.log('Enter a number to check if it a gapful number. Enter q to exit.');
process.stdin.on('data', (data) => {
if (isNaN(data)) {
console.log('Enter a integer');
}
if (data == 'q\n') {
process.exit();
}
let inputValue = data.toString().trim();
let divisor = inputValue[0] + inputValue[inputValue.length - 1];
if (parseInt(inputValue) % parseInt(divisor) === 0) {
return printLog(inputValue, true);
}
printLog(inputValue, false);
})
function printLog(inputValue, isGapful) {
console.log(`${inputValue} is ${isGapful ? '' : 'not'} a gapful number`);
}
Comments
Post a Comment