Create a Matlab Code which will convert from decimal to Binary which will accept
ID: 645599 • Letter: C
Question
Create a Matlab Code which will convert from decimal to Binary which will accept non-integer values and will account for repeating segments such as 0.77 and 0.68.
So far, I have the following code, however, it wont accept non-integer values.
function bin = dec_bin(dec)
i=0;
n=1;
while i == 0
if dec/2 > 0
if dec/2 ~= round(dec/2)
bin(n) = 1;
end
if dec/2 == round(dec/2)
bin(n) = 0;
end
end
if dec/2 < 1
bin(n) = 1;
i = 1;
else
n=n+1;
dec = floor(dec/2);
end
end
newbin=zeros(1,length(bin));
for i = 1:length(bin)
newbin(i) = bin((length(bin)-i)+1);
end
if length(bin) < 8
bin = [zeros(1,8-length(bin)), newbin];
else
bin = newbin;
end
fprintf('The conversion for the Decimal Value in Binary is %i', bin)
Explanation / Answer
Hope it may helps you..