You were just hired for a summer internship with one Atlanta\'s best software co
ID: 3624126 • Letter: Y
Question
You were just hired for a summer internship with one Atlanta's best software companies; however, on your first day of work you learn that for the next 3 months, the only job you will have is to convert binary (base 2) numbers into decimal numbers (base 10). You decide to write a script that will repetitively ask the user for a binary number and return its decimal equivalent until an illegal number (one containing digits other than 0 or 1) is entered. The number entered should contain only the digits 0 and 1. The rightmost digit has the value 2 and the digit N places to the left of that has the value 2N. For example: entering 110101 returns 53 = 2s + 24 + 22 + 20 You mast use iteration to solve this problem. Note. The input (...) function prompts the user for a value and returns the numerical result.Explanation / Answer
Dear, Here is the m file code Let us consider that input binary number is considerd as string Reading input using input function % variable binary_num is used to store binary number %variable decimal to store deimal value binary_num = input('Enter your binary number: ', 's');decimal = 0;
% loop iterates for each element of string binary_num
% length function return the size of string binary_num
for i = 1 : length(bin_nr)
decimal = decimal + str2num(binary_num(i)) * 2^(length(binary_num) - i);
end
decimal
Hope this will help you
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.