29. JavaScript Program to find the number of digits of a given number.
How does this program work?
- This program is used to find the number of digits of a given number using JavaScript.
- The integer entered by the user is stored in variable n.
- Here we are using while loop and this loop will be iterated upto n not equal to zero, once n will be zero the loop is terminated.
Here is the code
<html>
<head>
<title>JavaScript program to find the number of digits of a given number</title>
</head>
<body>
<table>
<tr>
<td> <input type="text" name="a" id="first" placeholder="Enter any number"> </td>
</tr>
<tr>
<td> <button onclick="digits ()" >Submit</button> </td>
</tr>
</table>
<div id="num"></div>
</body>
<script type="text/javascript">
function digits()
{
var n,i,digits = 0;
n = parseInt(document.getElementById ("first").value);
while(n != 0)
{
n = parseInt(n/10);
digits++;
}
document.getElementById ("num").innerHTML = "Total number of digits : "+ digits;
}
</script>
</html>