summaryrefslogtreecommitdiff
path: root/273/CH25/EX25.20
diff options
context:
space:
mode:
authorpriyanka2015-06-24 15:03:17 +0530
committerpriyanka2015-06-24 15:03:17 +0530
commitb1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b (patch)
treeab291cffc65280e58ac82470ba63fbcca7805165 /273/CH25/EX25.20
downloadScilab-TBC-Uploads-b1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b.tar.gz
Scilab-TBC-Uploads-b1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b.tar.bz2
Scilab-TBC-Uploads-b1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b.zip
initial commit / add all books
Diffstat (limited to '273/CH25/EX25.20')
-rwxr-xr-x273/CH25/EX25.20/ex2520.sce75
1 files changed, 75 insertions, 0 deletions
diff --git a/273/CH25/EX25.20/ex2520.sce b/273/CH25/EX25.20/ex2520.sce
new file mode 100755
index 000000000..4bbcf47fc
--- /dev/null
+++ b/273/CH25/EX25.20/ex2520.sce
@@ -0,0 +1,75 @@
+clc;//clears the command window
+clear;//clears all the variables
+i=1;w=1;
+bin=1011.01101;//Given binary number which we need to be convert into octal
+
+//conversion to decimal first
+temp1=floor(bin);//separating integer part from the given number
+temp2=modulo(bin,1);//separating decimal part from the given number
+temp2=temp2*10^5;//converting decimal value to integer for convenience
+while(temp1>0)//storing each integer digit in vector for convenience
+ p(i)=modulo(temp1,10);
+ temp1=floor(temp1/10);
+ i=i+1;
+end
+while(temp2>0)//storing each decimal digit in vector for convenience
+ q(w)=modulo(temp2,2);
+ temp2=(temp2/10);
+ temp2=floor(temp2);
+ w=w+1;
+end
+temp1=0;//flag bit
+for i=1:length(p)//checking whether it is a binary number or not
+ if(p(i)>1) then
+ disp('not a binary number');
+ abort;
+ end
+end
+for i=1:length(p)
+//multipliying bits of integer part with their position values and adding
+ temp1=temp1+(p(i)*2^(i-1));
+end
+temp2=0;//flag bit
+for z=1:length(q)
+//multipliying bits of decimal part with their position values and adding
+ temp2=temp2+(q(z)*2^(-1*(6-z)));
+end
+dec=temp1+temp2;
+//finally adding both the integer and decimal parts to get decimal equivalent
+
+//conversion from decimal to octal
+
+format('v',8);//making the default precision to 8 significant digits
+i=1;w=1;
+
+temp=modulo(dec,1);//separating decimal part from the given number
+temp2=floor(dec);//separating integer part from the given number
+
+
+while(temp2>0)//storing each integer digit in vector for convenience
+ r(i)=(modulo(floor(temp2),8))
+ temp2=floor(temp2/8);
+ i=i+1;
+end
+
+temp2=0;//clearing temporary variable 'temp2'
+
+for j=1:length(r)
+//multipliying bits of integer part with their position values and adding
+ temp2=temp2+(r(j)*10^(j-1));
+end
+
+while(temp~=0) //storing each decimal digit in vector for convenience
+ temp=temp*8;
+ s(w)=floor(temp);
+ w=w+1;
+ temp=modulo(temp,1);
+end
+
+temp1=0; //flag bit
+for k=1:length(s)
+//multipliying bits of decimal part with their position values and adding
+ temp1=temp1+(10^(-1*k)*s(k));
+end
+temp3=temp2+temp1;
+disp(temp3,'octal number is');