A Small Project: Sharpening All Your Knowledge

Final project: FizzBuzz, the Fibonacci sequence, and a prime number check — combining variables, expressions, loops, and functions.

A Small Project: Sharpening All Your Knowledge

You've learned all the ingredients: variables, types, operators, strings, if, for, while, and functions. Now it's time to put them into practice in three small projects — classics that every programmer has gone through.

Project 1: FizzBuzz

The rules: for the numbers 1 to 20 —

  • multiples of 3 → print Fizz
  • multiples of 5 → print Buzz
  • multiples of 3 and 5 → print FizzBuzz
  • otherwise → print the number
for i 1..21 {
    if i % 15 == 0 {
        print_str("FizzBuzz\n");
    } else {
        if i % 3 == 0 {
            print_str("Fizz\n");
        } else {
            if i % 5 == 0 {
                print_str("Buzz\n");
            } else {
                print(i);
            }
        }
    }
}

The core logic is in the remainder % and the nested branches — two concepts you learned in lessons 4 and 6.

Project 2: The Fibonacci Sequence

The Fibonacci sequence: each number is the sum of the two preceding ones — 0, 1, 1, 2, 3, 5, 8, ...

let a = 0;
let b = 1;
let berikutnya = 0;

for i in 0..10 {
    print(a);

    berikutnya = a + b;
    a = b;
    b = berikutnya;
}

Output:

0
1
1
2
3
5
8
13
21
34

The key: keeping the last two values and shifting them each round. Try tracing the first two or three rounds with a pencil — understand every step.

Project 3: Prime Number Check

A prime number: divisible only by 1 and itself.

fn is_prima(n) {
    let hasil = 1;

    let pembagi = 2;
    while pembagi < n {
        if n % pembagi == 0 {
            hasil = 0;
        }
        pembagi = pembagi + 1;
    }

    hasil
}

for i 2..20 {
    if is_prima(i) {
        print_str("prima: ");
        print(i);
    }
}

Output:

prima: 2
prima: 3
prima: 5
prima: 7
prima: 11
prima: 13
prima: 17
prima: 19

Note: if n % pembagi == 0 then n is not prime — we mark it by setting hasil to 0. This is the "flag" pattern that's very common in programming.

Extra Challenges

  1. Factorization: print all the prime factors of 84 (hint: combine is_prima with %).
  2. Perfect numbers: find a number below 100 whose divisors (excluding itself) sum to the number itself. 6 = 1 + 2 + 3 — 6 is the first perfect number.
  3. Star pyramid: print a 5-row pyramid of the character * — each row one longer than the previous (print_str("*\n"), "**\n", ...). Hint: use nested for loops and an accumulating string variable (baris = baris + "*").

After This Course

You now know the basics of writing AHA! Lang code! The next steps:

If you're a Python user curious about how AHA! Lang compares with Python, read our blog article: AHA! Lang vs Python.

Happy coding! 🎉