Page 372 - Web_Application_v2.0_C12_Fb
P. 372
2. Write JavaScript code that defines a function automorphic(number) which takes an integer as input and
prints whether the number is an automorphic number or not.
Hint: An automorphic number is a number whose square ends with the number itself. For example, 25
is automorphic because 25² = 625.
Ans. <!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE>AUTOMORPHIC NUMBER CHECKER</TITLE>
</HEAD>
<BODY>
<H1>AUTOMORPHIC NUMBER CHECKER</H1>
<SCRIPT>
function checkAutomorphic() {
let num = document.getElementById('NUMBERINPUT').value;
let result = document.getElementById('RESULT');
num = parseInt(num);
let square = num * num;
let numberStr = num.toString();
let squareStr = square.toString();
let endPart = squareStr.substring(squareStr.length - numberStr.length);
if (endPart === numberStr) {
result.textContent = num + " is an Automorphic number.";
}
else {
result.textContent = num + " is not an Automorphic number.";
}};
</SCRIPT>
<LABEL FOR="NUMBERINPUT">ENTER A NUMBER: </LABEL>
<INPUT TYPE="NUMBER" ID="NUMBERINPUT">
<BUTTON ONCLICK="checkAutomorphic()">CHECK</BUTTON>
<P ID="RESULT"></P>
</BODY>
</HTML>
Output of the preceding code is as follows:
370 Touchpad Web Applications (Ver. 2.0)-XII

