InterviewSolution
| 1. |
Write PL/SQL program to convert each digit of a given number into its corresponding word format. |
|
Answer» DECLARE-- declare necessary VARIABLES -- num represents the GIVEN number -- number_to_word represents the word format of the number -- str, len and digit are the intermediate variables used for program executionnum INTEGER; number_to_word VARCHAR2(100); digit_str VARCHAR2(100); len INTEGER; digit INTEGER; BEGIN num := 123456; len := LENGTH(num); dbms_output.PUT_LINE('Input: ' ||num); -- Iterate through the number one by one FOR i IN 1..len LOOP digit := SUBSTR(num, i, 1); -- Using DECODE, get the str REPRESENTATION of the digit SELECT Decode(digit, 0, 'Zero ', 1, 'One ', 2, 'Two ', 3, 'Three ', 4, 'Four ', 5, 'Five ', 6, 'Six ', 7, 'Seven ', 8, 'Eight ', 9, 'Nine ') INTO digit_str FROM dual; -- Append the str representation of digit to final result. number_to_word := number_to_word || digit_str; END LOOP; dbms_output.PUT_LINE('Output: ' ||number_to_word); END; Input: 12345 |
|