Reverse a Number in PL/SQL


PL/SQL is a block-structured language that combines SQL's functionality with procedural commands. In this article, we will discuss a program in PL/SQL to reverse a given number for example −

Input : 98765
Output : 56789
Explanation : reverse number of 98765 is 56789.

Input : 56784
Output : 48765
Explanation Reverse number of ‘56784’ is ‘48765’.

Approach to find The Solution

  • Take out the last digit from the number by finding the remainder of num/10.
  • Now add the last digit to another variable reversed_num.
  • Now check if num becomes 0 −
    • If YES, then go to STEP1.
    • If NO, then go to STEP4.
  • Finally, print the reversed_num.

Example

DECLARE
num number;
reverse_num number:=0;

begin
num:=98765;
while num>0
loop
reverse_num:=(reverse_num*10) + mod(num,10);
num:=trunc(num/10);
end loop;

dbms_output.put_line(' Reversed number is : '|| reverse_num);

Output

Reversed number is: 56789

Conclusion

In this article, we discuss the PL/SQL programming language, which is very simple to use; it feels like using ordinary English to command the system. We also discussed a problem of reversing a number in PL/SQL language. We hope you find this article helpful.

Updated on: 08-Mar-2023

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements