Wednesday 28 August 2013

Decimal to Roman Converter using PLSQL Block

Hello Friends,

     Sharing the code for converting decimal number to Roman number.
     Hope you can make it simpler so waiting for you suggestions.

DECLARE
   TYPE letters IS VARRAY(18) OF VARCHAR2(4);
   TYPE numbers IS VARRAY(18) OF INTEGER;
   letter_name              letters;
   number_calc            numbers;
   total                         INTEGER;
   ln_num                     INTEGER  := &Enter_Number;
   lv_string                   VARCHAR2(200);
BEGIN

   letter_name := letters('M', 'CM', 'D', 'CD', 'C','XC','L','XL','X','IX','VIII','VII','VI','V','IV','III','II','I');
                                 --Stored array of Number for reference
   number_calc:= numbers(1000, 900, 500, 400, 100,90,50,40,10,9,8,7,6,5,4,3,2,1);
                                 --Stored Number to deal with
   total := letter_name.count;
 
   FOR i IN 1 .. total LOOP
       WHILE   ln_num >= number_calc(i) LOOP
                        --Used WHILE LOOP to repeat the given number if it is greater than number_calc(i)
     
            DBMS_OUTPUT.PUT( letter_name(i) );
                         --Printing the number one after other so first stored using DBMS_OUTPUT.PUT()
            ln_num := (ln_num-number_calc(i));
       
       END LOOP;
    END LOOP;          
   DBMS_OUTPUT.NEW_LINE;      
                         --At a time printing all output coming from FOR Loop
END;
/

Decimal To Binary Converter Function using PLSQL

Hi Friends,
        Following is the code for converting decimal value into binary.
         Hope this code will help you to reduce your work.

             I have created a procedure which will help for direct conversion.

   CREATE OR REPLACE FUNCTION apps.decimal_to_binary ( ln_num      NUMBER)
   RETURN NUMBER 
   IS      
                ln_unit      NUMBER;
                ln_input    NUMBER;
                i               NUMBER  := 0;
                ln_out      NUMBER  := 0;

   BEGIN   
                ln_input := ln_num;         --User Value is stored in variable ln_input

               WHILE ln_input > 0            --Condition to check for value is greater than zero or not

                 LOOP

                       ln_unit    := MOD( ln_input, 2 );       --To get the unit digit multiple for Binary creation
                       ln_input  := TRUNC(ln_input/2);      --To get reaming amount after each binary division
                       ln_out := TO_NUMBER(ln_unit * power(10,i) + ln_out);        --To frame a Binary Number 
                       i:= i+1;

                 END LOOP;   

               RETURN(ln_out);
    END;
    /