6.1.3 Converting algorithms to programs

From Algorithm to Code

An algorithm is a step-by-step description of how to solve a problem, expressed in pseudocode or a flowchart. Converting it to a program means translating each algorithmic construct into the corresponding syntax of a programming language. The logic remains identical — only the notation changes.

Key constructs to translate: variable assignment, input/output, selection (if/elif/else), count-controlled repetition (for), condition-controlled repetition (while), and subprogram calls.

Worked Example 1: Pseudocode → Code

Algorithm: accept a positive integer from the user and output whether it is prime.

Pseudocode

n = input("Enter a number: ")
n = int(n)
is_prime = True
if n < 2:
    is_prime = False
else:
    for i in range(2, n):
        if n % i == 0:
            is_prime = False
if is_prime:
    output n, "is prime"
else:
    output n, "is not prime"
n = int(input("Enter a number: "))
is_prime = True
if n < 2:
    is_prime = False
else:
    for i in range(2, n):
        if n % i == 0:
            is_prime = False
if is_prime:
    print(n, "is prime")
else:
    print(n, "is not prime")
int n = int.Parse(Console.ReadLine());
bool isPrime = true;
if (n < 2) {
    isPrime = false;
} else {
    for (int i = 2; i < n; i++) {
        if (n % i == 0) isPrime = false;
    }
}
if (isPrime)
    Console.WriteLine(n + " is prime");
else
    Console.WriteLine(n + " is not prime");
Dim n As Integer = Integer.Parse(Console.ReadLine())
Dim isPrime As Boolean = True
If n < 2 Then
    isPrime = False
Else
    For i As Integer = 2 To n - 1
        If n Mod i = 0 Then isPrime = False
    Next
End If
If isPrime Then
    Console.WriteLine(n & " is prime")
Else
    Console.WriteLine(n & " is not prime")
End If

Worked Example 2: Flowchart → Code

Flowchart describes: ask for a PIN, compare to stored PIN, allow up to 3 attempts, lock if all fail.

CORRECT_PIN = "1234"
MAX_ATTEMPTS = 3
attempts = 0
access_granted = False

while attempts < MAX_ATTEMPTS and not access_granted:
    pin = input("Enter PIN: ")
    if pin == CORRECT_PIN:
        access_granted = True
        print("Access granted")
    else:
        attempts = attempts + 1
        print("Incorrect. Attempts remaining:", MAX_ATTEMPTS - attempts)

if not access_granted:
    print("Account locked")
const string CORRECT_PIN = "1234";
const int MAX_ATTEMPTS = 3;
int attempts = 0;
bool accessGranted = false;

while (attempts < MAX_ATTEMPTS && !accessGranted) {
    string pin = Console.ReadLine();
    if (pin == CORRECT_PIN) {
        accessGranted = true;
        Console.WriteLine("Access granted");
    } else {
        attempts++;
        Console.WriteLine("Incorrect. Attempts remaining: " + (MAX_ATTEMPTS - attempts));
    }
}
if (!accessGranted)
    Console.WriteLine("Account locked");
Const CORRECT_PIN As String = "1234"
Const MAX_ATTEMPTS As Integer = 3
Dim attempts As Integer = 0
Dim accessGranted As Boolean = False

Do While attempts < MAX_ATTEMPTS AndAlso Not accessGranted
    Dim pin As String = Console.ReadLine()
    If pin = CORRECT_PIN Then
        accessGranted = True
        Console.WriteLine("Access granted")
    Else
        attempts += 1
        Console.WriteLine("Incorrect. Attempts remaining: " & (MAX_ATTEMPTS - attempts))
    End If
Loop
If Not accessGranted Then
    Console.WriteLine("Account locked")
End If

 Key Takeaways

  • Converting an algorithm to code maps each construct directly: assignment → assignment statement, selection → if/elif/else, count loop → for, condition loop → while.
  • The logic is preserved; only the notation changes to match the language's syntax.
  • Pseudocode input() always returns a string in Python — explicit conversion with int() or float() is required.
  • Named constants (ALL_CAPS) should be used for values that are fixed and meaningful, like CORRECT_PIN and MAX_ATTEMPTS.