Impact-Site-Verification: dbe48ff9-4514-40fe-8cc0-70131430799e

Search This Blog

#Day83#100DaysChallenge- Matlab Loops | Binary to decimal Conversion

#Day83-Binary to Decimal Conversion

Task:

Write a code to Convert given binary number to decimal number
0000=0
0001=1
0010=2


Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those.

Matlab code

function decimal_value= binary_conversion(x)

count=numberofdigits(x);

pow=0;

decimal_value=0;

for i=1:1:count

    n=mod(x,10);

    decimal_value=decimal_value+(n*2^pow);

    pow=pow+1;

    x=floor(x/10);

end

end

Sample Input and Output

>> decimal_value= binary_conversion(0101)


decimal_value =


     5


>> binary_conversion(11011001)


ans =


   217


Click here for Video description





Live Support: t.me/matlabirawen




 

 

   

 


No comments

Popular Posts

Followers