summaryrefslogtreecommitdiff
path: root/2699/CH13/EX13.31/Ex13_31.sce
blob: 51c3698bd1563d412baa1287217d53fc09151ae3 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//Ex13_31 PG-13.17
clc
clear
printf("\n  i)\n Conversion of binary number 101101.1101 to\n\n")

//conversion into decimal form
N=(1*2^5)+(0*2^4)+(1*2^3)+(1*2^2)+(0*2^1)+(1*2^0)+(1*2^(-1))+(1*2^(-2))+(0*2^(-3))+(1*2^(-4));
printf(" Decimal form = %.4f\n",N)

//Conversion into hexadecimal form
x=floor(N);//separating the integer part from the decimal part
b=dec2hex(x);
z=modulo(N,1);
d=0;
//converting the values after the decimal point into hexadecimal
//first we convert into decimal form and then into hexadecimal form
for i=2:5;//converting the values after the decimal point into hexadecimal
    z=z*16;
    q=floor(z);
    d=d+q/(10^i);
    if z>=1 then
        z=z-q;
    end
end
   if (d==.10)
           a=['A']
           end     
   if (d==.11)
           a=['B']
           end
   if (d==.12)
           a=['C']
           end
   if (d==.13)
           a=['D']
           end
   if (d==.14)
           a=['E']
           end
   if (d==.15)
           a=['F'] 
           end        
printf(" Hexadecimal form = %s.%s\n",b,a)

//conversion into octal form
z=modulo(N,1)
x=floor(N);//separating the decimal from the integer part
b=0;
c=0;
d=0;
while(x>0)  //taking integer part into a matrix and convert to equivalent binary
y=modulo(x,8);
b=b+(10^c)*y;
x=x/8;
x=floor(x);
c=c+1;
end
for i=1:10;//converting the values after the decimal point intooctal
    z=z*8;
    q=floor(z);
    d=d+q/(10^i);
    if z>=1 then
        z=z-q;
    end
end
s=b+d;
printf(" octal  form = %.2f\n\n",s);
printf("\n  ii)\n Conversion of binary number 11011011.100101 to\n\n")

//conversion into decimal form
N=(1*2^7)+(1*2^6)+(0*2^5)+(1*2^4)+(1*2^3)+(0*2^2)+(1*2^1)+(1*2^0)+(1*2^(-1))+(0*2^(-2))+(0*2^(-3))+(1*2^(-4))+(0*2^(-5))+(1*2^(-6));
printf(" Decimal form =%.6f\n\n",N)

//Conversion into hexadecimal form
x=floor(N);//separating the integer part from the decimal part
b=dec2hex(x);
z=modulo(N,1);//first we convert into decimal form and then into hexadecimal form
d=0;
//converting the values after the decimal point into hexadecimal
for i=1:10;//converting the values after the decimal point into hexadecimal
    z=z*16;
    q=floor(z);
    d=d+q/(10^i);
    if z>=1 then
        z=z-q;
    end
end
printf(" Hexadecimal form\n    integer part 11011011 =  %s \n    ",b)
printf("decimal part 100101   =%.2f\n\n",d)

//conversion into octal form
z=modulo(N,1)
x=floor(N);//separating the decimal from the integer part
b=0;
c=0;
d=0;
while(x>0)  //taking integer part into a matrix and convert to equivalent binary
y=modulo(x,8);
b=b+(10^c)*y;
x=x/8;
x=floor(x);
c=c+1;
end
for i=1:10;//converting the values after the decimal point intooctal
    z=z*8;
    q=floor(z);
    d=d+q/(10^i);
    if z>=1 then
        z=z-q;
    end
end
s=b+d;
printf(" octal  form =%.2f",s);