Day 73: Convert Binary to Decimal Number of 100DaysCodingChallenge
Hello all,
#day73
Day73
Today's my task was to Write a Program to Convert Binary to Decimal number manually by creating user-defined functions.
I have started the 100 days of coding challenge to improve my programming skills..
Code :
// JavaScript program to convert binary to decimal // Function to convert binary to decimal function binaryToDecimal(n) { let num = n; let dec_value = 0; // Initializing base value to 1, i.e 2^0 let base = 1; let temp = num; while (temp) { let last_digit = temp % 10; temp = Math.floor(temp / 10); dec_value += last_digit * base; base = base * 2; } return dec_value; } // Driver program to test above function //let num = 10101001; let num = prompt("Enter a number : "); console.log("The Decimal Number is ", binaryToDecimal(num) + " "); // This code
Output :
Happy Coding:)
Thank you...
Comments
Post a Comment