Loading...
Loading...
COBOL is a legacy language still used extensively in banking and enterprise systems. Two fundamental looping constructs in COBOL are:
while loop).for loop).Your task is to simulate the behavior of both COBOL loop constructs in a modern language and determine what value a given accumulator holds after each loop completes.
You are given a string describing a COBOL-style loop operation. Simulate the described loop and return the final value of the accumulator (ACC) after the loop terminates.
Each loop description follows one of two formats:
Format 1 — PERFORM UNTIL:
UNTIL counter=<stop> step=<step> start=<start> acc_op=<ADD|MULT>
Simulate: start counter at start, each iteration add step to counter and apply acc_op to accumulator (initially 0 for ADD, 1 for MULT), until counter equals stop.
Format 2 — PERFORM VARYING:
VARYING start=<start> by=<by> until=<until> acc_op=<ADD|MULT>
Simulate: counter starts at start, each iteration apply acc_op to accumulator using the current counter value, then increment counter by by, until counter >= until.
Accumulator operations:
ADD: accumulator starts at 0; each iteration, ACC = ACC + counterMULT: accumulator starts at 1; each iteration, ACC = ACC * counterInput: A single string describing the loop (see formats above).
Output: A single integer — the final value of ACC after the loop terminates.
Input: UNTIL counter=5 step=1 start=1 acc_op=ADD
Output: 10
Explanation: Counter starts at 1. Loop runs while counter != 5. Iterations: counter=1 (ACC=1), counter=2 (ACC=3), counter=3 (ACC=6), counter=4 (ACC=10). Counter becomes 5, loop stops. Final ACC = 10.
Input: VARYING start=1 by=1 until=5 acc_op=MULT
Output: 24
Counter starts at 1. Each iteration multiplies ACC by counter then increments. Iterations: counter=1 (ACC=1), counter=2 (ACC=2), counter=3 (ACC=6), counter=4 (ACC=24). Counter becomes 5 (>= until=5), loop stops. Final ACC = 24.
1 <= start <= 100 1 <= step/by <= 50 start < stop (for UNTIL) or start < until (for VARYING) stop - start <= 1000 (prevents infinite loops) All values are positive integers acc_op is either ADD or MULT
Input: VARYING start=2 by=2 until=10 acc_op=ADD
Output: 20
Explanation: Counter starts at 2, increments by 2. Iterations: counter=2 (ACC=2), counter=4 (ACC=6), counter=6 (ACC=12), counter=8 (ACC=20). Counter becomes 10 (>= 10), loop stops. Final ACC = 20.