How to use naive method In Javascript
The most straightforward way to check if a number is a neon number is to square the number, calculate the sum of the digits of the square, and then compare the sum to the original number.
Example: To check whether a number is neon or not using a naive approach.
Javascript
function isNeonNumber(num) { let square = num * num; let sumOfDigits = 0; while (square > 0) { sumOfDigits += square % 10; square = Math.floor(square / 10); } return sumOfDigits === num; } console.log(isNeonNumber(9)); console.log(isNeonNumber(8)); |
true false
JavaScript Program to Check whether the Input Number is a Neon Number
A neon number is a number where the sum of digits of its square equals the number itself. For example, 9 is a neon number because 9^2 = 81, and the sum of the digits 81 is 9. There are several ways to check if a given number is a neon number in JavaScript or not which are as follows:
Table of Content
- Using naive method
- Using string conversion
- Using array methods