The logic looks correct except for one minor logic error in:
sum=sum+(i*rem);
which should read
sum=sum+i*r;
You need to make sure the statements execute correctly in Matlab syntax.
For example, an integer division can be written as
dec=dec./2;
or
dec=idivide(dec,2);
Also, be careful about overflow of the decimal representation of the binary number (sum), which has at least 3 times more digits as the decimal equivalent.
How to convert from decimal to binary in matlab.....this is what I have so far.....
dec = 43;
i = 1;
sum=0;
while dec>0
r = rem(dec, 2);
sum=sum+(i*rem);
dec=dec/2;
i=i*10;
end
sum
1 answer