How To Create Perfect Number Checker
Submitted by alpha_luna on Tuesday, April 26, 2016 - 15:15.
Related Code: How To Check The Odd And Even Number
In this article, we are going to learn on How To Create Perfect Number Checker using JavaScript. The user type into the textbox a numerical value then our program will check the given number if it is a Perfect Number or Not. Also, we have an alert message to check the values if it is valid or invalid.
HTML Source Code
This HTML code where the user types their numeric value to check if it is a Perfect Number or Not.
JavaScript
This script will use to determine the value if it is a Perfect Number or Not.
And, this is the style we are going to use for the layout.
Output:
If the given value is Perfect Number.
If the given value is Not Perfect Number.
Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.
- <table cellpadding="5" cellspacing="5" width="100%">
- <tr>
- <td class="table_1">
- Enter a Number
- <input type="text" id="userEnter_Value" class="txtBox_number" autofocus="autofocus" />
- </td>
- </tr>
- <tr>
- </tr>
- <tr>
- <td style="text-align:center;">
- <input type="submit" class="button_style" value="Check" onclick="check_prime()" />
- <input type="submit" class="button_style1" value="Clear" onclick="clear_textbox()" />
- </td>
- </tr>
- </table>
- <script type="text/javascript">
- function check_PerfectNumber() {
- var input = document.getElementById("userEnter_Value").value;
- var number = parseInt(input);
- if (isNaN(number)) {
- alert("Please enter a number.");
- document.getElementById("userEnter_Value").value="";
- document.getElementById("result").innerHTML = "";
- document.getElementById("userEnter_Value").focus();
- }
- else if (input.length === 0) {
- alert("Please enter a valid input");
- document.getElementById("userEnter_Value").focus();
- }
- else if (!isNaN(number)) {
- if (is_perfect(number)) {
- document.getElementById("result").innerHTML = number + " is a PERFECT number.";
- }
- else {
- document.getElementById("result").innerHTML = number + " is NOT a PERFECT number.";
- }
- }
- else {
- document.getElementById("result").innerHTML = "Please enter a number.";
- }
- }
- function is_perfect(number)
- {
- var temp = 0;
- for(var i=1;i<=number/2;i++)
- {
- if(number%i === 0)
- {
- temp += i;
- }
- }
- if(temp === number)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- function clearTextbox_Value(){
- document.getElementById("result").innerHTML = "";
- document.getElementById("userEnter_Value").value="";
- document.getElementById("userEnter_Value").focus();
- }
- </script>
- <style type="text/css">
- body {
- width:400px;
- margin:auto;
- }
- .table_1 {
- text-align: center;
- color: blue;
- font-size: 18px;
- font-weight: bold;
- font-family: cursive;
- }
- .txtBox_number {
- width: 150px;
- font-size: 18px;
- border: blue 1px solid;
- background: azure;
- text-align: center;
- cursor: pointer;
- }
- .button_style {
- font-size: 18px;
- border: blue 1px solid;
- background: azure;
- color: blue;
- width: 100px;
- padding: 5px;
- cursor: pointer;
- margin-right:20px;
- }
- .button_style1 {
- font-size: 18px;
- border: red 1px solid;
- background: azure;
- color: red;
- width: 100px;
- cursor: pointer;
- padding: 5px;
- }
- #result {
- margin-top: 40px;
- font-size: 18px;
- font-family: cursive;
- color: blue;
- padding: 10px;
- text-align: center;
- border: red 1px solid;
- }
- </style>
Add new comment
- 38 views