IDENTIFICATION DIVISION.
 PROGRAM-ID. FIZZ-BUZZ.
 AUTHOR. P. M. Campbell
 DATE-WRITTEN. 2020-04-10
 REMARKS. Implementing the Fizz Buzz algorithm in COBOL, compiled with Open Cobol on Ubuntu 18.04

DATA DIVISION.
 FILE SECTION.
 WORKING-STORAGE SECTION.
   01 COUNTR PIC 999 value 0 .
   01 REM3 PIC 99.
   01 ZRO PIC 99 value 0.
   01 DIV3 PIC 99.
   01 REM5 PIC 99.
   01 DIV5 PIC 99.
   01 MSG PIC X(8).

PROCEDURE DIVISION.
 DISPLAY 'COBOL, OMG, blast from the past!'
 
 PERFORM VARYING COUNTR FROM 1 BY 1 UNTIL COUNTR > 100
   INITIALIZE MSG
   DIVIDE COUNTR BY 3 GIVING DIV3 REMAINDER REM3 
   DIVIDE COUNTR BY 5 GIVING DIV5 REMAINDER REM5 
   IF REM3 IS EQUAL TO  0 OR REM5 IS EQUAL TO ZRO  THEN
        PERFORM CHECKFIZZBUZZ
	DISPLAY MSG
   ELSE 
        DISPLAY COUNTR   
   END-IF
 END-PERFORM.     
 STOP RUN.

CHECKFIZZBUZZ.
	IF REM3 IS EQUAL TO 0  THEN
	   MOVE "Fizz" TO MSG
	END-IF.
    	IF REM5 IS EQUAL TO 0  THEN
	    STRING MSG DELIMITED BY SPACE "Buzz " DELIMITED BY SPACE INTO MSG
			ON OVERFLOW DISPLAY "OVERFLOW error"
        END-IF.
	
TESTCHECKFIZZBUZZ.
	DISPLAY "3: " REM3 " 5: " REM5
	IF REM3 IS EQUAL TO 0  THEN
	   MOVE "Fizz" TO MSG
	ELSE 
	   INITIALIZE MSG	
        END-IF.
        DISPLAY "MSG " MSG.
	IF REM5 IS EQUAL TO 0  THEN
	    STRING MSG DELIMITED BY SPACE "Buzz " DELIMITED BY SPACE INTO MSG
			ON OVERFLOW DISPLAY "OVERFLOW error"
	ELSE 
	   INITIALIZE MSG
        END-IF.
	DISPLAY "3 " MSG.

