function GCD(a, b) ! The greatest common denominator (GCD) is the largest positive integer ! that divides into both numbers without a remainder. ! Examples: GCD(256,64)=64, GCD(12,8)=4, GCD(5,3)=1 integer x, y integer a, b, g x = a y = b ! Work with absolute values (positive integers) if (x .lt. 0) x = -x if (y .lt. 0) y = -y if (x + y .gt. 0) then g = y ! Iterate until x = 0 do while (x .gt. 0) g = x x = mod(y, x) y = g enddo GCD = g else ! Error, both parameters zero GCD = 0 endif end
Hide code
Visustin flow chart for Fortran