Telkom Pseudocode

Samples

Complete programs that pass the checker, in roughly the order the course teaches them. Open one in the editor to run it, then choose Edit a copy to change it.

  1. Hello

    Asks for your name with input() and prints a greeting.

    Asks you for input when it runs.

    Open in the editor
    program halo
    kamus
      nama : string
    algoritma
      output("Siapa namamu?")
      input(nama)
      output("Halo, " + nama + "!")
    endprogram
  2. Input

    Reads two integers and prints their sum, difference, product, div, and mod.

    Asks you for input when it runs.

    Open in the editor
    program aritmetika_input
    kamus
      a, b : integer
    algoritma
      output("Masukkan dua bilangan bulat:")
      input(a, b)
      output("Jumlah:", a + b)
      output("Selisih:", a - b)
      output("Kali:", a * b)
      output("Bagi (div):", a div b)
      output("Sisa (mod):", a mod b)
    endprogram
  3. Control flow

    Demonstrates if/else if, case, for, while, and repeat-until loops.

    Open in the editor
    program alur_kendali
    kamus
      nilai, i, jumlah : integer
    algoritma
      nilai = 3
      if nilai > 5 then
        output("besar")
      else if nilai == 3 then
        output("tiga")
      else
        output("kecil")
      endif
      case nilai of
        1:
          output("satu")
        2, 3:
          output("dua atau tiga")
        else:
          output("lainnya")
      endcase
      jumlah = 0
      for i = 1 to 5 do
        jumlah = jumlah + i
      endfor
      output("jumlah for:", jumlah)
      while jumlah > 0 do
        jumlah = jumlah - 3
      endwhile
      output("sisa while:", jumlah)
      repeat
        jumlah = jumlah + 1
      until jumlah >= 3
      output("hasil repeat:", jumlah)
    endprogram
  4. Arrays

    Fills an integer array, sums its elements, and finds the maximum value.

    Open in the editor
    program larik
    kamus
      constant N : integer = 5
      arr : array[0..N-1] of integer
      i, jumlah, maks : integer
    algoritma
      for i = 0 to N - 1 do
        arr[i] = (i + 1) * 10
      endfor
      jumlah = 0
      maks = arr[0]
      i = 0
      while i < N do
        jumlah = jumlah + arr[i]
        if arr[i] > maks then
          maks = arr[i]
        endif
        i = i + 1
      endwhile
      output("jumlah:", jumlah)
      output("maksimum:", maks)
    endprogram
  5. Strings

    Shows string length, character indexing, character replacement, and concatenation.

    Open in the editor
    program olah_string
    kamus
      s : string
      c : char
    algoritma
      s = "telkom"
      output("panjang:", len(s))
      c = s[1]
      output("indeks 1:", c)
      s[0] = "T"
      output("ubah:", s)
      output("gabung:", s + " Bandung")
    endprogram
  6. Structs

    Defines a record type with fields and stores multiple records in an array.

    Open in the editor
    program tipe_bentukan
    kamus
      type Mahasiswa : <
        nama : string,
        umur : integer
      >
      daftar : array[0..1] of Mahasiswa
      i : integer
    algoritma
      daftar[0].nama = "Aep"
      daftar[0].umur = 20
      daftar[1].nama = "Siti"
      daftar[1].umur = 19
      for i = 0 to 1 do
        output(daftar[i].nama, daftar[i].umur)
      endfor
    endprogram
  7. Procedures

    Passes variables by reference using in/out parameters to swap and increment values.

    Open in the editor
    program prosedur_tukar
    kamus
      a, b : integer
    algoritma
      a = 10
      b = 20
      output("sebelum:", a, b)
      tukar(a, b)
      output("sesudah:", a, b)
      tambah_satu(a)
      output("ditambah 1:", a)
    endprogram
    
    procedure tukar(in/out x, in/out y : integer)
    { I.S. x dan y terdefinisi
      F.S. nilai x dan y bertukar }
    kamus
      t : integer
    algoritma
      t = x
      x = y
      y = t
    endprocedure
    
    procedure tambah_satu(in/out v : integer)
    { I.S. v terdefinisi
      F.S. v bertambah 1 }
    algoritma
      v = v + 1
    endprocedure
  8. Recursion

    Computes factorial and Fibonacci numbers using recursive functions.

    Open in the editor
    program rekursi
    kamus
      n : integer
    algoritma
      n = 6
      output("faktorial:", faktorial(n))
      output("fibonacci:", fibonacci(n))
    endprogram
    
    function faktorial(n : integer) -> integer
    { Mengembalikan nilai n! secara rekursif }
    algoritma
      if n <= 1 then
        return 1
      endif
      return n * faktorial(n - 1)
    endfunction
    
    function fibonacci(n : integer) -> integer
    { Mengembalikan suku ke-n barisan Fibonacci secara rekursif }
    algoritma
      if n <= 1 then
        return n
      endif
      return fibonacci(n - 1) + fibonacci(n - 2)
    endfunction
  9. Linked list

    Builds a singly linked list with allocate, traverses and searches it, and frees all nodes.

    Open in the editor
    program senarai_berantai
    kamus
      type Infotype : integer
      type Address : pointer to ElmList
      type ElmList : <
        info : Infotype,
        next : Address
      >
      type List : <
        first : Address
      >
      L : List
      p, q : Address
      jumlah : integer
    algoritma
      Create_List(L)
      p = New_Element(10)
      L.first = p
      q = New_Element(20)
      p->next = q
      p = New_Element(30)
      q->next = p
      jumlah = 0
      p = L.first
      while p != NIL do
        output("isi:", p->info)
        jumlah = jumlah + p->info
        p = p->next
      endwhile
      output("jumlah:", jumlah)
      output("ada 20:", search(L, 20) != NIL)
      p = L.first
      while p != NIL do
        q = p->next
        deallocate(p)
        p = q
      endwhile
      L.first = NIL
    endprogram
    
    procedure Create_List(in/out L : List)
    { I.S. -
      F.S. Pointer first dari List L bernilai NIL }
    algoritma
      L.first = NIL
    endprocedure
    
    function New_Element(data : Infotype) -> Address
    { Mengembalikan pointer ke elemen baru teralokasi }
    kamus
      p : Address
    algoritma
      allocate(p)
      p->info = data
      p->next = NIL
      return p
    endfunction
    
    function search(L : List, v : Infotype) -> Address
    { Mengembalikan pointer ke elemen berisi v jika ditemukan, atau NIL jika tidak ada }
    kamus
      p : Address
    algoritma
      p = L.first
      while p != NIL and p->info != v do
        p = p->next
      endwhile
      return p
    endfunction