4. While loops in MATLAB
Get a binary from the user by input. And turn it into the decimal using while loop. (Hint: store the input binary as a character array and perform operation on each element). Finally, check the answer of your script with bin2dec.
bin = input("Enter the binary number: ","s"); dec = 0; temp = 1; i=length(bin); while i>=1 if(bin(i) == '1') dec = dec + temp; elseif(bin(i) == '0') dec = dec + 0; else fprintf("Invalid input\n"); return; end temp = temp * 2; i = i - 1; end fprintf("The number in decimal is %d\n",dec);
Get Answers For Free
Most questions answered within 1 hours.