000010 IDENTIFICATION DIVISION. 000020 PROGRAM-ID. GCD. 000030 000040 DATA DIVISION. 000050 WORKING-STORAGE SECTION. 000060 01 X PIC 9999999. 000070 01 Y PIC 9999999. 000080 01 G PIC 9999999. 000081 01 U PIC 9999999. 000090 000100 PROCEDURE DIVISION. 000110 000120 DISPLAY "X? " 000130 ACCEPT X. 000140 DISPLAY "Y? " 000150 ACCEPT Y. 000160 PERFORM GCD. 000170 DISPLAY "GCD = " G. 000180 EXIT PROGRAM. 000190 000200 CALCULATION-SECTION. 000210 GCD. 000221* The greatest common denominator (GCD) is the largest positive integer 000222* that divides into both numbers without a remainder. 000223* Examples: GCD(256,64)=64, GCD(12,8)=4, GCD(5,3)=1 000224* Input values: X, Y (will be modified) 000225* Output value: G 000226 000227* Return zero on error. 000230 MOVE 0 TO G. 000232* Require positive integers 000240 IF X <= 0 THEN COMPUTE X = -X. 000243 IF Y <= 0 THEN COMPUTE Y = -Y. 000246 000247 MOVE Y TO G 000248 *> Iterate until x = 0 000249 PERFORM UNTIL X = 0 000250 MOVE X TO G 000251 *> Get remainder of Y/X 000252 DIVIDE Y BY X GIVING U REMAINDER X 000253 *> Update return value 000254 MOVE G TO Y 000300 END-PERFORM. 000310 000320 EXIT-GCD. 000330 EXIT.
Hide code
Visustin flow chart for COBOL