From b1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b Mon Sep 17 00:00:00 2001 From: priyanka Date: Wed, 24 Jun 2015 15:03:17 +0530 Subject: initial commit / add all books --- 1871/CH8/EX8.19/Ch08Ex19.sce | 64 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100755 1871/CH8/EX8.19/Ch08Ex19.sce (limited to '1871/CH8/EX8.19/Ch08Ex19.sce') diff --git a/1871/CH8/EX8.19/Ch08Ex19.sce b/1871/CH8/EX8.19/Ch08Ex19.sce new file mode 100755 index 000000000..6d455a1d1 --- /dev/null +++ b/1871/CH8/EX8.19/Ch08Ex19.sce @@ -0,0 +1,64 @@ +// Scilab code Ex8.19 : Pg:346(2008) +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 octal = decimal_octal(n) // Function to convert decimal to octal + i=1; octal = 0; + while (n<>0) + rem = n-fix(n./8).*8; + octal = octal + rem*i; + n = int(n/8); + i = i*10; + end +endfunction + +function hex = decimal_hex(n) // Function to convert decimal to hexadecimal + hex = emptystr(); + while (n <>0) + rem = n-fix(n./16).*16; + if rem == 10 then + hex(i)=hex+'A'; + elseif rem == 11 then + hex=hex+'B'; + elseif rem == 12 then + hex=hex+'C'; + elseif rem == 13 then + hex=hex+'D'; + elseif rem == 14 then + hex=hex+'E'; + elseif rem == 15 then + hex=hex+'F'; + else + hex=hex+string(rem); + end + n = int(n/16); + end + hex = strrev(hex); // Reverse string +endfunction + +n = [32, 256, 51]; // Initialize a vector to the given decimals +printf("\n__________________________________________________"); +printf("\nDecimal Binary Octal Hexadecimal "); +printf("\n__________________________________________________"); +for i = 1:1:3 +printf("\n%d %10d %5d %4s", n(i), decimal_binary(n(i)), decimal_octal(n(i)), decimal_hex(n(i))); +end +printf("\n__________________________________________________"); + +// Result +// __________________________________________________ +// Decimal Binary Octal Hexadecimal +// __________________________________________________ +// 32 100000 40 20 +// 256 100000000 400 100 +// 51 110011 63 33 +// __________________________________________________ -- cgit