Learn Rpg - 10 Code Examples & CST Typing Practice Test
RPG (Report Program Generator) is a high-level programming language primarily used for business applications, especially on IBM midrange systems like AS/400 and IBM i, for generating reports and handling data processing tasks.
Learn RPG with Real Code Examples
Updated Nov 20, 2025
Code Sample Descriptions
RPG Counter and Theme Toggle
D COUNT S 5I 0 INZ(0)
D ISDARK S 1A INZ('0')
C *ENTRY PLIST
C PARM
C UPDATEUI BEGSR
C WRITE 'Counter:' COUNT
C IF ISDARK = '1'
C WRITE 'Theme: Dark'
C ELSE
C WRITE 'Theme: Light'
C ENDIF
C ENDSR
C INCREMENT BEGSR
C COUNT += 1
C PERFORM UPDATEUI
C ENDSR
C DECREMENT BEGSR
C COUNT -= 1
C PERFORM UPDATEUI
C ENDSR
C RESET BEGSR
C COUNT = 0
C PERFORM UPDATEUI
C ENDSR
C TOGGLETHEME BEGSR
C IF ISDARK = '0'
C ISDARK = '1'
C ELSE
C ISDARK = '0'
C ENDIF
C PERFORM UPDATEUI
C ENDSR
C *MAIN BEGSR
C PERFORM UPDATEUI
C PERFORM INCREMENT
C PERFORM INCREMENT
C PERFORM TOGGLETHEME
C PERFORM DECREMENT
C PERFORM RESET
C ENDSR
Demonstrates a simple counter with theme toggling using RPG variables and WRITE statements.
RPG Fibonacci Sequence
D A S 5I 0 INZ(0)
D B S 5I 0 INZ(1)
D C S 5I 0
D I S 5I 0
C *ENTRY PLIST
C PARM
C WRITE A
C WRITE B
C FOR I = 1 TO 8
C C = A + B
C WRITE C
C A = B
C B = C
C ENDFOR
Generates the first 10 Fibonacci numbers.
RPG Factorial Calculator
D N S 5I 0 INZ(5)
D F S 10I 0 INZ(1)
D I S 5I 0
C *ENTRY PLIST
C PARM
C FOR I = 1 TO N
C F = F * I
C ENDFOR
C WRITE F
Calculates factorial of a number.
RPG Prime Checker
D N S 5I 0 INZ(13)
D ISPRIME S 1A INZ('Y')
D I S 5I 0
C *ENTRY PLIST
C PARM
C FOR I = 2 TO N-1
C IF N MOD I = 0
C ISPRIME = 'N'
C LEAVE
C ENDIF
C ENDFOR
C IF ISPRIME = 'Y'
C WRITE 'Prime'
C ELSE
C WRITE 'Not Prime'
C ENDIF
Checks if a number is prime.
RPG Sum of Array
D ARR S 5I 0 DIM(5) INZ(1:2:3:4:5)
D SUM S 10I 0 INZ(0)
D I S 5I 0
C *ENTRY PLIST
C PARM
C FOR I = 1 TO 5
C SUM += ARR(I)
C ENDFOR
C WRITE SUM
Calculates the sum of an array.
RPG Reverse String
D STR S 20A INZ('HELLO')
D REV S 20A INZ('')
D I S 5I 0
C *ENTRY PLIST
C PARM
C FOR I = %LEN(STR) TO 1 BY -1
C REV = REV + %SUBST(STR:I:1)
C ENDFOR
C WRITE REV
Reverses a string.
RPG Multiplication Table
D N S 5I 0 INZ(5)
D I S 5I 0
C *ENTRY PLIST
C PARM
C FOR I = 1 TO 10
C WRITE N:' x ':I:' = ':N*I
C ENDFOR
Prints multiplication table of a number.
RPG Temperature Converter
D C S 10I 0 INZ(25)
D F S 10I 0
C *ENTRY PLIST
C PARM
C F = (C * 9 / 5) + 32
C WRITE F
Converts Celsius to Fahrenheit.
RPG Simple Alarm Simulation
D TEMP S 5I 0 INZ(80)
D THRESH S 5I 0 INZ(75)
C *ENTRY PLIST
C PARM
C IF TEMP > THRESH
C WRITE 'Alarm: Temperature Too High!'
C ELSE
C WRITE 'Temperature Normal'
C ENDIF
Simulates an alarm when temperature exceeds threshold.
RPG Random Walk Simulation
D STEPS S 5I 0 INZ(10)
D POS S 5I 0 INZ(0)
D I S 5I 0
C *ENTRY PLIST
C PARM
C FOR I = 1 TO STEPS
C IF %RAND < 0.5
C POS += 1
C ELSE
C POS -= 1
C ENDIF
C WRITE POS
C ENDFOR
Simulates a 1D random walk.
Frequently Asked Questions about Rpg
What is Rpg?
RPG (Report Program Generator) is a high-level programming language primarily used for business applications, especially on IBM midrange systems like AS/400 and IBM i, for generating reports and handling data processing tasks.
What are the primary use cases for Rpg?
Generating business reports. Batch data processing. Financial calculations. Inventory and logistics management. Legacy enterprise application maintenance
What are the strengths of Rpg?
Highly optimized for business data processing. Legacy systems are stable and reliable. Deep integration with IBM i/AS400. Efficient for batch report generation. Strong backward compatibility
What are the limitations of Rpg?
Steep learning curve for modern programmers. Legacy syntax can be cumbersome (fixed format). Limited use outside IBM i ecosystem. Modern web and GUI support is limited. Difficult to integrate with non-IBM systems
How can I practice Rpg typing speed?
CodeSpeedTest offers 10+ real Rpg code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.