ADAMA SCIENCE AND TECHNOLOGY UNIVERSITY
SCHOOL OF ELECTRICAL ENGINEERING AND COMPUTING
ELECTRICAL POWER AND CONTROL ENGINEERING PROGRAM
COMPUTATIONAL METHODS ASSIGNMENT -1
NAME ABONEH TESHITA
ID UGR/30094/15
SECTION 3
### Algorithm for Converting Base 2 to Base 10:
1. Input the binary number as a string.
2. Initialize the decimal result to 0.
3. Determine the length of the binary string.
4. Loop through each digit in the binary string from right to left:
- Convert the character digit to a number.
- If the digit is '1', calculate \(2^{\text{position}}\) and add it to the decimal result. The position starts
at 0 from the rightmost digit.
5. Return the decimal result.
### MATLAB Code for Converting Base 2 to Base 10:
function decimal = binaryToDecimal(binaryStr)
% Initialize the decimal result
decimal = 0;
% Get the length of the binary string
n = length(binaryStr);
% Loop through each digit in the binary string from right to left
for i = 1:n
% Get the digit (as a character)
digit = binaryStr(n - i + 1);
% Convert the character to a number
digitValue = str2double(digit);
% Add the value to the decimal result if the digit is 1
if digitValue == 1
decimal = decimal + 2^(i - 1);
end
end
end
### Example Usage of the Code:
binaryStr = '1101'; % Binary number as a string
decimalResult = binaryToDecimal(binaryStr);
disp(['The decimal equivalent of ', binaryStr, ' is ', num2str(decimalResult)]);
Output:
The decimal equivalent of 1101 is 13
### Explanation:
- The algorithm provides a step-by-step procedure to understand the process of conversion from base 2
to base 10.
- The MATLAB code implements this algorithm in the MATLAB programming language, allowing you to
execute it and get the conversion result.