Functions

Master modular programming with parameters and return values

3 Easy 4 Medium 3 Hard ~80 min total
1

Count to Ten

Easy 5 min

Problem:

Display numbers from 1 to 10 using a FOR loop.

Example Output:

Sample Run
1 2 3 4 5 6 7 8 9 10

Solution

Algorithm CountToTen
    For i = 1 To 10
        Print i
    Endfor
Endalgorithm
Key Concepts:
  • FOR loop syntax: For i = start To end
  • Loop variable auto-increments
  • Endfor marks loop end
2

Sum of N Numbers

Easy 6 min

Problem:

Calculate the sum of first N natural numbers using a loop.

Sample Run
Enter N: 5 Sum of first 5 numbers: 15

Solution

Algorithm SumOfN
    var n = Input "Enter N:"
    var sum = 0
    
    For i = 1 To n
        sum = sum + i
    Endfor
    
    Print "Sum of first", n, "numbers:", sum
Endalgorithm
Key Concepts:
  • Accumulator pattern (sum variable)
  • Initialize accumulator before loop
  • Update accumulator in each iteration
3

Multiplication Table

Easy 7 min

Problem:

Display the multiplication table of a given number.

Sample Run
Enter number: 5 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 ... 5 x 10 = 50

Solution

Algorithm MultiplicationTable
    var number = Input "Enter number:"
    
    For i = 1 To 10
        var result = number * i
        Print number, "x", i, "=", result
    Endfor
Endalgorithm
Key Concepts:
  • FOR loop for fixed iterations
  • Calculations inside loop body
  • Loop variable used in calculations
4

Factorial Calculator

Medium 8 min

Problem:

Calculate factorial of a number (n! = n × (n-1) × (n-2) × ... × 1).

Sample Run
Enter number: 5 5! = 120

Solution

Algorithm Factorial
    var n = Input "Enter number:"
    var factorial = 1
    
    For i = 1 To n
        factorial = factorial * i
    Endfor
    
    Print n, "! =", factorial
Endalgorithm
Key Concepts:
  • Multiplicative accumulator pattern
  • Initialize to 1 (not 0) for multiplication
  • Common mathematical algorithm
5

Print Even Numbers

Medium 7 min

Problem:

Print all even numbers from 1 to N.

Sample Run
Enter N: 10 2 4 6 8 10

Solution

Algorithm PrintEvenNumbers
    var n = Input "Enter N:"
    
    For i = 1 To n
        If i mod 2 == 0 Then
            Print i
        Endif
    Endfor
Endalgorithm
Key Concepts:
  • Loop with conditional (IF inside FOR)
  • Modulo operator for even checking
  • Selective printing based on condition
6

Countdown Timer

Medium 6 min

Problem:

Create a countdown from N to 1 using a WHILE loop.

Sample Run
Enter starting number: 5 5 4 3 2 1 Blast off!

Solution

Algorithm Countdown
    var n = Input "Enter starting number:"
    
    While n > 0
        Print n
        n = n - 1
    Endwhile
    
    Print "Blast off!"
Endalgorithm
Key Concepts:
  • WHILE loop with condition
  • Decrementing counter
  • Condition checked before each iteration
7

Pattern Printing

Medium 8 min

Problem:

Print a pattern of stars (* * * * *).

Sample Run
Enter number of rows: 5 * * * * * * * * * * * * * * *

Solution

Algorithm StarPattern
    var rows = Input "Enter number of rows:"
    
    For i = 1 To rows
        var line = ""
        For j = 1 To i
            line = line + "* "
        Endfor
        Print line
    Endfor
Endalgorithm
Key Concepts:
  • Nested loops (loop inside loop)
  • String concatenation in loops
  • Outer loop controls rows, inner controls columns
8

Fibonacci Sequence

Hard 10 min

Problem:

Generate first N numbers of Fibonacci sequence (0, 1, 1, 2, 3, 5, 8...).

Sample Run
Enter N: 7 0 1 1 2 3 5 8

Solution

Algorithm Fibonacci
    var n = Input "Enter N:"
    var a = 0
    var b = 1
    
    For i = 1 To n
        Print a
        var temp = a + b
        a = b
        b = temp
    Endfor
Endalgorithm
Key Concepts:
  • Sequence generation using loops
  • Multiple variables tracking state
  • Classic algorithm pattern
9

Prime Number Checker

Hard 12 min

Problem:

Check if a number is prime using a loop to test divisibility.

Sample Run
Enter number: 17 17 is a prime number

Solution

Algorithm PrimeChecker
    var number = Input "Enter number:"
    var isPrime = True
    
    If number <= 1 Then
        isPrime = False
    Else
        For i = 2 To number - 1
            If number mod i == 0 Then
                isPrime = False
            Endif
        Endfor
    Endif
    
    If isPrime Then
        Print number, "is a prime number"
    Else
        Print number, "is not a prime number"
    Endif
Endalgorithm
Key Concepts:
  • Loop for testing multiple conditions
  • Boolean flag to track result
  • Early detection but loop continues
10

Multiplication Table Grid

Hard 12 min

Problem:

Create a multiplication table grid (NxN) using nested loops.

Sample Run
Enter size: 3 1 2 3 2 4 6 3 6 9

Solution

Algorithm MultiplicationGrid
    var size = Input "Enter size:"
    
    For i = 1 To size
        var row = ""
        For j = 1 To size
            row = row + (i * j) + "  "
        Endfor
        Print row
    Endfor
Endalgorithm
Key Concepts:
  • 2D data with nested loops
  • Both loop variables used in calculation
  • Building strings row by row
  • Grid/matrix generation
Back to Exercise Hub