Reverse a number in PL/SQL Last Updated : 20 Nov, 2019 Comments Improve Suggest changes Like Article Like Report Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Explanation: Consider the example, input = 12345. Step 1 : mod(12345,10) = 5 rev:= 0*10 + 5 = 5 num = floor(12345/10) = 1234 Step 2 : mod(1234,10) = 4 rev:= 5*10 + 4 = 54 num = floor(1234/10) = 123 Step 3 : mod(123,10) = 3 rev:= 54*10 + 3 = 543 num = floor(123/10) = 12 Step 4 : mod(12,10) = 2 rev:= 543*10 + 2 = 5432 num = floor(12/10) = 1 Step 5 : mod(1,10) = 1 rev:= 5432*10 + 1 = 54321 num = floor(1/10) = 0 in step 5, num =0 which doesn't satisfy the while condition and loop terminates. rev = 54321 More examples: Input : 123456 Output :654321 Input :87459 Output :95478 Below is the required implementation: SQL SET SERVEROUTPUT ON; DECLARE -- declare a number 'num' for reading actual input -- declare another number 'rev' that would be reverse of num num NUMBER; rev NUMBER; BEGIN -- & is used to read input from keyboard num:=# -- initialize rev to 0 rev:=0; -- the loop runs until num is greater than 0 WHILE num>0 LOOP -- mod function is used to find the modulus/ remainder of num when divided by 10 rev:=(rev*10) + mod(num,10); -- floor function is used to obtain a result which is an integer num:=floor(num/10); END LOOP; DBMS_OUTPUT.PUT_LINE('Reverse of the number is: ' || rev); END; / -- Program End Output: Enter value for num : 157439 Reverse of the number is: 934751 Comment More infoAdvertise with us Next Article Reverse a number in PL/SQL S Shashank12 Follow Improve Article Tags : Misc DBMS SQL SQL-PL/SQL Practice Tags : Misc Similar Reads Prime number in PL/SQL Prerequisite â PL/SQL introductionA prime number is a whole number greater than 1, which is only divisible by 1 and itself. First few prime numbers are : 2 3 5 7 11 13 17 19 23 â¦..In PL/SQL code groups of commands are arranged within a block. A block group-related declarations or statements. In decl 1 min read Reverse a string in PL/SQL Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Given a string, the task is to reverse a string using P 1 min read Program for Fibonacci numbers in PL/SQL The Fibonacci numbers are the numbers in the following integer sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, â¦â¦.. In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation Fn = Fn-1 + Fn-1 with seed values F0= 0 and F1 = 1. Given a number n, print n- 1 min read How to Print Prime Numbers in MS SQL Server? In this article, we are going to print Prime numbers using MS SQL. Here we will be using 2 while loops statement for printing prime numbers. Steps 1: First we will DECLARE a variable I with initial value 2. Query: DECLARE @I INT=2Step 2: Then we will DECLARE a variable PRIME with an initial value of 3 min read Factorial of a number in PL/SQL Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Here, first, we take three variables num, fact, and tem 1 min read Like