%THIS FUNCTION TAKES A USER INPUT IN DECIMAL AND OUTPUTS ITS BINARY
%CONVERSION
%OTERO, ALIANJECON
%2025/5/29
clc, clear, close all;

function dec2bin(num)
w = floor(num);
d = num - w;

binw = [];
bind = [];

if w == 0
    binw = [0];
else
    while w ~= 0
        binw = [mod(w,2),binw];
        w = floor(w/2);
    end
end

miters = 50;
if d ~= 0
for i = 1:miters
    d2 = d * 2;
    bind = [bind,floor(d2)];
    d = d2 - floor(d2);

    if d == 0
        break
    end
end
end

fprintf("%d, Decimal Representation\n",num)

for i = 1:size(binw,2)
    fprintf("%d",binw(i));
end

if ~isempty(bind)
fprintf(".");

for i = 1:size(bind,2)
    fprintf("%d",bind(i));
end
end

fprintf(", Binary Representation");

end

num = input("Number\n");

dec2bin(num);