blob: 71874555a05eb7f5642c5eebb25b952470e97776 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
// Scilab code Ex15.7 : Pg:773(2011)
clc;clear;
function [bini]= decimal_binary(ni) // Function to convert decimal to binary
bini = 0;
i = 1;
while (ni <> 0)
rem = ni-fix(ni./2).*2;
ni = int(ni/2);
bini = bini + rem*i;
i = i * 10;
end
endfunction
function [deci]= binary_decimal(ni) // Function to convert binary to decimal
deci = 0;
i = 0;
while (ni <> 0)
rem = ni-fix(ni./10).*10;
ni = int(ni/10);
deci = deci + rem*2.^i;
i = i + 1;
end
endfunction
num1 = 11101; // Initialize the first binary number
num2 = 10111; // Initialize the second binary number
printf("%6d + %6d = %7d", num1, num2, decimal_binary(binary_decimal(num1)+binary_decimal(num2)));
// Result
// 11101 + 10111 = 110100
|