blob: 3700404b0c25b4ac0d8ced4d056af427d96d2cf0 (
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
//example 2.3 (b)//
clc
//clears the command window//
clear
//clears//
p =1;
//initialising//
q =1;
z =0;
b =0;
w =0;
f =0;
//bin= input ( ” Enter the binary no to be converted to its decimal equivalent : ”)
//accepting the binary input from user//
bin =1100.1011;
d = modulo(bin,1) ;
//separating the decimal part and the integer part//
d= d *10^10;
a = floor(bin) ;
//removing the decimal part//
while (a >0)
// Loop to take the binary bits of integer into a matrix//
r = modulo (a ,10) ;
b(1,q) = r ;
a=a/10;
a=floor( a ) ;
q = q +1;
end
for m =1: q -1
// multipliying the bits of integer with their position values and adding//
c=m -1;
f=f+b(1,m)*(2^c);
end
while (d >0)
// Loop to take the binary bits of decimal into a matrix//
e = modulo (d ,2)
w (1 , p ) = e
d = d /10;
d = floor ( d )
p = p +1;
end
for n =1: p -1
//multipliying the bits of decimal with their position values and adding//
z = z + w (1 , n ) *(0.5) ^(11 - n ) ;
end
z = z *10000;
//rounding of to 4 decimal values//
z = round ( z ) ;
z = z /10000;
x=f+z;
disp('The Decimal equivalent of the Binary number given is');
disp(x);
//Displaying the final result//
|