How to Create Converter for Decimal to Binary

In the previous tutorial, we create a simple Converter For Binary To Decimal. Today, we create another converter this is an alternative of the previous tutorial. And it's called Converter for Decimal to Binary. This converter works by typing a numeric value from the user then the user clicks the "Ok" button to convert the Decimal Value to in Binary Form. This program also creates using JavaScript. You can also check the live demo of this simple tutorial, so you can get an idea and you can try this out, let's start coding. JavaScript Source Code This script will ask the user to input the numerical value to convert decimal to binary.
  1. <script>
  2. function decimalConvert_toBinary(dec,length){
  3. var output_result = "";
  4. while(length--)
  5. output_result += (dec >> length ) & 1;
  6. return output_result;
  7. }
  8. var input = parseInt(prompt("Enter a Number"));
  9.  
  10. input_solve = decimalConvert_toBinary(input,8);
  11.  
  12. if (!isNaN(input)) {
  13. document.write("<h2><b style='color:red;'>" +input + "<sub>10</sub></b> in decimal is equivalent to <b style='color:blue;'>"
  14. + input_solve + "<sub>2</sub></b> in binary. </h2>" );
  15.  }
  16. while(isNaN(input)){
  17. input = parseInt(prompt("Please enter a numerical value."));
  18.  
  19. input_solve =decimalConvert_toBinary(input,8);
  20. if (!isNaN(input)) {
  21. document.write("<h2><b style='color:red;'>" +input + "<sub>10</sub></b> in decimal is equivalent to <b style='color:blue;'>"
  22. + input_solve + "<sub>2</sub></b> in binary.</h2>" );
  23.  }
  24. }
  25. </script>
Output: The user input the numerical value to convert decimal to binary. Result This is the output after the user gives the value to convert. Result Hope that this simple yet useful tutorials that I created may help you to your future projects. So what can you say about this work? Share your thoughts in the comment section below or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment