Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Converting a Binary Number to Decimal Problem Statement Given a binary number of

ID: 3751926 • Letter: C

Question

Converting a Binary Number to Decimal Problem Statement Given a binary number of length at most 6, convert it to the decimal representation. For example, the binary number 100 is 4 in decimal. Do not use existing matlab function to do the conversion. The functions allowed for this assignment are: input(), str2num() length() and fprintf). If you want to you functions not listed here, ask me for permission Input A binary number of length at most 6. But the length can be anything between 1 and 6 Output The number in decimal. If the length of the input is more than 6, give an error message Hint You can use the function length) to check the input length. You can use leading O's if the length of the input is less than 6 Sample Outputs & Test Cases Test1 Enter the binary number: 100 The number in decimal is 4 Test2 Enter the binary number: 100100 The number in decimal is 36 Test3 The length of the binary number can not exceed 6. 27 Program Complexity My solution code for this program is 13 lines.

Explanation / Answer

% matlab code for binary to deciaml conversion without using matlab function


% taking input as string
binary = input('Enter the binary number : ', 'str' );


% declaring and initializing a variable to store decimal value
decimal = 0;

% an iterator variable
it = 1;

%loop to traverse bits of binary number
while it < length(binary) + 1
    decimal += 2^(length(binary) - it) * str2num(binary(it)) ;
    it++;
end

%printing the decimal value
fprintf(' ')
fprintf('The number in decimal is ')
fprintf('%d ',decimal);

Thank you!

If you got any doubt in the solution please feel free to ask in the comment section.

Please do upvote!!