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;
/

No comments:

Post a Comment