ctf Lesson 4 58 min read

CTF: Loops

29 challenges — Week 4

Loops Challenges

29 challenges Week 4 Week 4 (C Control Flow, Functions, and Writing Real Programs)

Loop control structures in C: for loops, while loops, nested loops, and loop patterns (sentinel, fencepost, cumulative algorithms). Covers loop traces, output prediction, and real-world loop applications including ASCII art generation, digit processing, and pattern printing.


Difficulty Breakdown

Level Count
Beginner 11
Intermediate 17
Advanced 1

Challenges

1. Armstrong Numbers

Difficulty: Intermediate Time: ~15 min Type: code output

Print all n-digit Armstrong numbers (narcissistic numbers), where the sum of each digit raised to the power n equals the original number. For example, 153 is a 3-digit Armstrong number because 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153.

Concepts: for loops with range generation, while loops for digit extraction, modulo operator (%) for digit isolation, cumulative sum pattern, pow() function from

Practice on CodeStepByStep


2. Ascii Figure

Difficulty: Intermediate Time: ~20 min Type: code output

Draw an ASCII figure composed of /, *, and \ characters based on a size parameter. The figure has a diamond-like or decorative pattern where each line uses a formula to determine the count and type of characters.

Concepts: nested for loops, arithmetic expressions within loops, loop index usage in character/spacing calculations, console output (printf) with repeated characters, parameterized output based on input

Practice on CodeStepByStep


3. Biggest And Smallest

Difficulty: Beginner Time: ~10 min Type: interactive program

Prompt the user to enter n numbers, then print the largest and smallest values entered. This exercise practices the fencepost pattern where the first number initializes the min/max variables, and remaining numbers are compared.

Concepts: cumulative algorithm pattern (maintaining running min/max), fencepost pattern (handling first value specially), conditional logic with comparison operators (<, >), loop iteration with scanf for input, variable initialization and updates

Practice on CodeStepByStep


4. Blastoff

Difficulty: Beginner Time: ~5 min Type: code output

Print a countdown sequence from 10 to 1, formatted as ‘T-minus 10, 9, 8, … 1, blastoff!’

Concepts: for loop with decrement (i–), loop condition with >= comparison, loop index in output expressions, conditional formatting (comma only between numbers, not after last), string concatenation in output

Practice on CodeStepByStep


5. Book

Difficulty: Intermediate Time: ~25 min Type: code output

Draw a 3D book figure using nested loops and function decomposition. The book is drawn with a fixed SIZE constant (10) and is composed of separate functions for the spine, front cover, and 3D effect.

Concepts: function decomposition and modularity, nested for loops within functions, global constants (SIZE) for parameterization, loop formulas for alignment and repetition, console output coordination across functions

Practice on CodeStepByStep


6. Book2

Difficulty: Intermediate Time: ~20 min Type: code output

Enhanced version of the book exercise where the SIZE is a parameterized global constant. The program draws the same 3D book figure but with SIZE defined at the top, allowing easy resizing without modifying loop logic.

Concepts: global constants with #define, parameterization of ASCII art, scaling output based on single parameter, function modularity with constant-driven sizes, nested loops with SIZE-based calculations

Practice on CodeStepByStep


7. Compute Sum Of Digits

Difficulty: Beginner Time: ~10 min Type: interactive program

Prompt user for an integer, then compute and print the sum of its digits. Use a while loop with modulo and division to extract and sum individual digits.

Concepts: while loop condition (n > 0), modulo operator for digit extraction (n % 10), integer division for reducing number (n / 10), cumulative sum pattern, loop termination conditions

Practice on CodeStepByStep


8. Count Factors

Difficulty: Beginner Time: ~10 min Type: function output

Write a function that returns the count of factors (divisors) of a positive integer n. A factor is any positive integer that divides n evenly (n % factor == 0).

Concepts: for loop from 1 to n, modulo operator for divisibility testing (n % i == 0), cumulative count pattern, conditional increment (if condition then count++), function parameters and return values

Practice on CodeStepByStep


9. Fibonacci

Difficulty: Beginner Time: ~12 min Type: interactive program

Display the Fibonacci sequence (1, 1, 2, 3, 5, 8, 13, …) where each number is the sum of the two preceding numbers. Continue until the sequence exceeds a user-specified maximum value.

Concepts: while loop with numerical condition, variable assignment and updates, sequence generation (each term from previous two), variable swapping pattern (t1, t2, t3 rotation), cumulative output with formatting

Practice on CodeStepByStep


10. Fizzbuzz

Difficulty: Beginner Time: ~10 min Type: interactive program

Classic FizzBuzz problem: For integers 1 to n, print ‘Fizz’ if divisible by 3, ‘Buzz’ if divisible by 5, ‘FizzBuzz’ if divisible by both, or the number itself otherwise.

Concepts: for loop iteration from 1 to n, multiple modulo conditions (%, 3, 5, 15), nested if/else-if/else logic, importance of test order (15 before 3 or 5), conditional output

Practice on CodeStepByStep


11. Flip Coin Three Heads

Difficulty: Intermediate Time: ~15 min Type: interactive program

Simulate coin flips until three consecutive heads appear. Flip a virtual coin, track consecutive heads, and output the sequence until you get three heads in a row.

Concepts: while loop with custom termination condition, random number generation (rand(), srand()), modulo operator for booleanizing random values, state tracking (consecutive_heads counter), conditional logic for resetting state

Practice on CodeStepByStep


12. Floyds Triangle

Difficulty: Intermediate Time: ~15 min Type: code output

Print a k-row Floyd’s triangle where the first row has 1, second row has 2 3, third row has 4 5 6, etc. Each row contains incrementally numbered values.

Concepts: nested for loops, cumulative numbering across loop iterations, arithmetic formulas for row/column calculations, proper line breaks and spacing, loop index manipulation

Practice on CodeStepByStep


13. Loop Mystery Exam1

Difficulty: Beginner Time: ~8 min Type: code trace

Trace the execution of a while loop that performs integer division and modifies variables i and j, printing intermediate values. Return the sum i+j at the end.

Concepts: for/while loop mechanics, variable initialization and updates, conditional branching in loops, output statements (printf), loop termination conditions

Practice on CodeStepByStep


14. Loop Mystery Exam3

Difficulty: Intermediate Time: ~12 min Type: code trace

Trace a while loop that decrements y and subtracts y from x repeatedly, printing intermediate values. Return the original sum of x+y.

Concepts: nested loop execution order, variable scope and updates in nested contexts, complex conditional logic in loops, printf output sequencing, multi-variable tracking

Practice on CodeStepByStep


15. Loop Mystery Exam5

Difficulty: Advanced Time: ~15 min Type: code trace

Trace a while loop that divides x by z (using modulo check) and increments z, printing pairs until the modulo condition fails.

Concepts: complex nested control flow, variable interactions across loop scopes, off-by-one and boundary conditions, subtle conditional logic, loop invariants and loop state

Practice on CodeStepByStep


16. Loop Mystery Print1

Difficulty: Intermediate Time: ~10 min Type: code trace

Trace nested loops that print centered asterisks forming a triangle pattern (diamond half). Output grows from 1 star to 19 stars, centered.

Concepts: printf format specifiers (%d, %s, %c, etc.), loop variable values in output expressions, whitespace in output (spaces, tabs, newlines), printf behavior in loops, conditional output

Practice on CodeStepByStep


17. Number Loops1

Difficulty: Intermediate Time: ~12 min Type: code output

Write nested for loops to print each digit repeated on its own line: 1, 22, 333, 4444, 55555.

Concepts: nested for loops, loop index arithmetic for pattern generation, formula-based number output, spacing and alignment

Practice on CodeStepByStep


18. Number Loops2

Difficulty: Intermediate Time: ~12 min Type: code output

Write nested for loops to print dots followed by the row number digit: ….1, …22, ..333, .4444, 55555.

Concepts: nested for loops, complex loop index formulas, pattern analysis and decomposition, formula derivation from examples

Practice on CodeStepByStep


19. Number Loops3

Difficulty: Intermediate Time: ~15 min Type: code output

Write nested for loops to print an anti-diagonal pattern: dot, digit, dot on each line: ….1, …2., ..3.., .4…, 5….

Concepts: triple-nested or complex nested loops, multi-index arithmetic formulas, advanced pattern recognition, loop bound calculations

Practice on CodeStepByStep


20. Number Square

Difficulty: Intermediate Time: ~12 min Type: code output

Write an interactive program that prompts for minimum and maximum values, then prints a wrapping number square where numbers increment from min to max and wrap around using modulo arithmetic.

Concepts: nested for loops (2D iteration), 2D indexing and position-based calculations, formula derivation for grid values, row-major order printing

Practice on CodeStepByStep


21. Print Triangle

Difficulty: Beginner Time: ~8 min Type: code output

Write nested for loops to print a left-aligned triangle of hash symbols with 6 rows: #, ##, ###, ####, #####, ######.

Concepts: nested for loops, loop index used as count for repetition, character output (putchar or printf), newline handling

Practice on CodeStepByStep


22. Range Of Numbers

Difficulty: Beginner Time: ~5 min Type: code output

Write an interactive program that prompts for start and end values, then prints a comma-separated list of all integers in that range (ascending or descending based on direction).

Concepts: basic for loop syntax, loop bounds and conditions, printf with format specifiers, loop index in output

Practice on CodeStepByStep


23. Rocket

Difficulty: Intermediate Time: ~15 min Type: code output

Draw an ASCII rocket with SIZE=3 using helper functions draw_cone, draw_line, draw_half1, and draw_half2 to construct different parts of the rocket.

Concepts: nested for loops, loop formulas for spacing and character counts, multi-section figure composition, ASCII art design

Practice on CodeStepByStep


24. Rocket2

Difficulty: Intermediate Time: ~15 min Type: code output

Draw an ASCII rocket parameterized by a SIZE constant. Same structure as rocket but allows resizing by changing SIZE.

Concepts: parameterized loop bounds, scaling formulas based on parameter, function decomposition (optional), flexible ASCII art generation

Practice on CodeStepByStep


25. Roll Two Dice

Difficulty: Intermediate Time: ~12 min Type: interactive program

Write an interactive program that prompts the user for a desired sum, then rolls two dice (random values 1-6) repeatedly until the sum matches the desired total.

Concepts: while loop with custom termination, random number generation with range (1-6), multiple random values per iteration, conditional logic for state checking, counter/accumulator patterns

Practice on CodeStepByStep


26. Sentinel Sum

Difficulty: Beginner Time: ~10 min Type: interactive program

Write an interactive program that repeatedly prompts for numbers and adds them to a sum until the user enters -1 (sentinel value), then prints the total sum.

Concepts: sentinel pattern for input termination, while loop with break statement, cumulative sum pattern, input validation (checking for sentinel), loop termination by data value (not count)

Practice on CodeStepByStep


27. Space Needle

Difficulty: Intermediate Time: ~20 min Type: code output

Draw an ASCII Space Needle with SIZE=4 using helper functions: draw_antenna, draw_top1, draw_top2, draw_line, and draw_stem to construct the different sections.

Concepts: nested for loops for complex ASCII art, loop formulas for varying width, multi-section composition, spacing precision, symmetry and alignment

Practice on CodeStepByStep


28. Space Needle2

Difficulty: Intermediate Time: ~20 min Type: code output

Draw an ASCII Space Needle parameterized by a SIZE constant. Same structure as space_needle but allows resizing.

Concepts: parameterization of complex ASCII art, scaling multi-section figure, formula generalization, dependent calculations

Practice on CodeStepByStep


29. Stars Print

Difficulty: Beginner Time: ~10 min Type: code output

Trace three different nested loop code samples that produce ASCII art: A) 5x10 star grid, B) growing star triangle, C) number triangle.

Concepts: nested for loops, loop index in pattern calculations, character output (printf with * or similar), spacing and alignment

Practice on CodeStepByStep