Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

GCD of two numbers in PL/SQL

  • Last Updated : 12 Jul, 2018

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.

Attention reader! Don’t stop learning now. Learn SQL for interviews using SQL Course  by GeeksforGeeks.

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 variable num1, num2 and t 
    -- and these three variables datatype are integer  
    num1 INTEGER
    num2 INTEGER
    t    INTEGER
    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 ' 
                         ||' and ' 
                         ||' is ' 
-- Program End 

Output :

GCD of 8 and 48 is 8
My Personal Notes arrow_drop_up
Recommended Articles
Page :