summaryrefslogtreecommitdiff
path: root/462/CH6/EX6.4.b/ex_6_4_b.sce
blob: 5b70f3afb03981017c0bceb5bd6d7a198ec5b242 (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
//exmple 6 .4(b)//
clc
//clears the screen//
clear
//clears already existing variables//
a =0;
b =0;
q =0;
//bb=input(”Enter the first no (in decimal) : ” ) ;
//aa=input( ”Enter the number from which first no has to subtracted : ” ) ;
aa =8;
bb =1;
while ( aa >0)
//converting the inputs into binary numbers//
       x = modulo ( aa ,2) ;
       a = a + (10^ q ) * x ;
       aa = aa /2;
       aa = floor ( aa ) ;
       q = q +1;
end
nn = a
q =0;
while ( bb >0)
       x = modulo ( bb ,2) ;
       b = b + (10^ q ) * x ;
     bb = bb /2;

bb = floor ( bb ) ;
q = q +1;
end
printf ( ' \n The binary equivalent of first no is %f\n\n ' ,b ) ;
printf ( 'The binary equivalent of second no is %f\n\n' ,a ) ;
for i =1:40
      a1 ( i ) = modulo (a ,10) ;
      a = a /10;
      a = round ( a ) ;
      b1 ( i ) = modulo (b ,10) ;
      b = b /10;
      b = round ( b ) ;
end
bro (1) =0;
for i =1:40
     c1 ( i ) = a1 ( i ) - b1 ( i ) - bro ( i ) ;
     if c1 ( i ) == -1 then
            bro ( i +1) = 1;
            c1 ( i ) =1;
     elseif c1 ( i ) == -2 then
              bro ( i +1) = 1;
            c1 ( i ) =0;
     else
            bro ( i +1) =0;
     end
   end
   re =0;
   format (  'v' ,18) ;
   for i =1:40
         re = re +( c1 ( i ) *(10^( i -1) ) )
   end
   printf ( ' The difference of given two numbers is %f\n\n ' , re ) ;
q =1;
b =0;
f =0;
a = re ;
while (a >0)
//converting the binary result to decimal then to hexadecimal//
 r = modulo (a ,10) ;
 b (1 , q ) = r ;
 a = a /10;
 a = floor ( a ) ;
 q = q +1;
end
for m =1: q -1
       c =m -1
      f = f + b (1 , m ) *(2^ c ) ;
end
hex = dec2hex ( f ) ;
printf ( ' Difference in decimal notation is %d\n\n ' ,f ) ;
printf ( ' Difference in hexadecimal notation is %s \n ' , hex ) ;