GCD of two numbers 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 two numbers and task is to find the GCD (Greatest Common Divisor) or HCF (Highest Common Factor) value of the numbers.
Examples:
Input: num1 = 4, num2 = 6 Output: gcd of (num1, num2) = 2 Input: num1 = 8, num2 = 48 Output: gcd of (num1, num2) = 8
Approach is to take two numbers and find their GCD value using Euclidean algorithm.
Below is the required implementation:
DECLARE -- declare variable num1, num2 and t -- and these three variables datatype are integer num1 INTEGER; num2 INTEGER; t INTEGER; BEGIN num1 := 8; num2 := 48; WHILE MOD(num2, num1) != 0 LOOP t := MOD(num2, num1); num2 := num1; num1 := t; END LOOP; dbms_output.Put_line('GCD of ' ||num1 ||' and ' ||num2 ||' is ' ||num1); END; -- Program End |
Output :
GCD of 8 and 48 is 8
Recommended Posts:
- Difference between SQL and PLSQL
- Sum Of Two Numbers in PL/SQL
- Print all odd numbers and their sum from 1 to n in PL/SQL
- Print all even numbers from 1 to n in PL/SQL
- Swap two numbers in PL/SQL
- Sum and average of three numbers in PL/SQL
- Swap two numbers in PL/SQL without using temp
- Greatest number among three given numbers in PL/SQL
- Convert the given numbers into words in Pl/SQL
- Finding sum of first n natural numbers in PL/SQL
- Display Sequence of Numbers in SQL Using LEVEL
- SELECT INTO statement in SQL
- Pivot and Unpivot in SQL
- Common error in Group By
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



