Master Pseudocode with Pro
Get unlimited access to the AI Tutor, Code Generator, and Converters. Plus, enjoy an ad-free experience.
Common Pseudocode Examples & Algorithms
Learning the syntax of pseudocode is only half the battle; the real challenge lies in applying that syntax to solve complex problems. By studying established algorithms, you can train your brain to think computationally and structure your own code more efficiently.
In this guide, we have curated a massive collection of standard algorithms. From basic mathematical operations to complex sorting mechanics, these examples cover a wide array of logic paradigms. For absolute consistency, all examples in this guide are written in strict CIE (Cambridge) pseudocode standard.
Table of Contents
Mathematical Algorithms
These examples focus on using arithmetic operators (like MOD) and iteration to solve number-based problems.
1. Checking Even or Odd
Determining whether a number is even or odd is a classic introductory programming challenge. This simple algorithm leverages the Modulus (MOD) operator, which computes the remainder of a division operation. If a number divided by 2 leaves a remainder of exactly 0, it means the number is perfectly divisible by 2, classifying it as even. Conversely, any other remainder indicates an odd number.
Notice how we use an IF...THEN...ELSE selection statement to handle the two possible outcomes gracefully based on the boolean condition Number MOD 2 = 0.
2. Calculating Factorials
In mathematics, a factorial (denoted as n!) is the product of an integer and all the positive integers below it. For instance, 5! = 5 * 4 * 3 * 2 * 1 = 120. Calculating factorials is extremely common in permutations, combinations, and probability algorithms.
This example demonstrates a standard count-controlled FOR loop. The loop iterates from 1 up to the user-input number N. On each iteration, the Total variable is multiplied by the loop counter i, accumulating the final large sum step-by-step.
3. Fibonacci Sequence Generator
The Fibonacci sequence is a famous series of numbers where each subsequent number is found by adding up the two numbers right before it, resulting in the pattern: 0, 1, 1, 2, 3, 5, 8, 13, and so on. Generating this sequence is a robust way to practice managing and updating multiple variables simultaneously.
As you can see in the pseudocode below, the algorithm initializes the first two numbers (0 and 1). Then, a FOR loop calculates the NextNum while strategically re-assigning Num1 and Num2 to shift the calculation window forward. This generator efficiently outputs the first 10 numbers in the sequence.
Searching Algorithms
Searching arrays is a foundational skill in computer science, heavily testing your ability to use boolean flags and bounded iteration safely.
4. Linear Search
A linear search provides the most straightforward method for finding a specific target item in an unsorted array or list. It works methodically by checking every single element, one by one, from the beginning to the end, until the target is found or the list is exhausted.
This implementation uses a pre-condition WHILE loop coupled with a boolean Found flag. This is a crucial optimization: as soon as the element is discovered, the flag is flipped to TRUE, instantly breaking out of the loop and preventing unnecessary checks. This technique is highly tested in secondary school computing exams.
5. Finding the Maximum Value
Finding the highest (or lowest) number in a dataset is an incredibly common operation required in formatting data, building leaderboards, or analyzing sensor inputs.
The most reliable strategy is an assumption-based approach. We first assume that the item at the very first index (MyList[1]) is our "current maximum". We then iterate through the remainder of the array using a FOR loop. If any subsequent element proves to be larger than our stored MaxVal, we overwrite the stored variable. Once the loop concludes, we are guaranteed to be left with the absolute maximum value.
Sorting Algorithms
6. Bubble Sort
Bubble sort is often the first sorting algorithm taught to computer science students because of its intuitive, albeit inefficient, logic. It works by repeatedly stepping through the list, comparing adjacent elements, and swapping them if they are in the wrong order. This causes the largest elements to "bubble" to the top (or end) of the array with each pass.
The pseudocode utilizes a classic nested FOR loop structure. The outer loop controls the number of passes needed, while the inner loop handles the element-by-element comparisons. Notice how a temporary variable (Temp) is absolutely necessary to successfully swap two elements without overwriting and losing one of their values.
7. Insertion Sort
Insertion sort is another fundamental sorting algorithm, but it generally performs much better than bubble sort on smaller or partially sorted lists. It approaches sorting similarly to how you might arrange playing cards in your hands: building the final sorted array one item at a time.
In this algorithm, the outer FOR loop iterates through the unsorted section of the array. For each element (CurrentValue), an inner WHILE loop shifts all larger elements to the right to create a "gap". Once the correct position is found, the CurrentValue is inserted. This shifting technique is a hallmark of insertion sort algorithms.
Validation and Processing
8. Password Length Validation
Validating user input is a critical security and usability requirement for any application. A ubiquitous exam question requires you to "trap" a user in a continuous loop until they provide data that meets specific criteria—known as a validation check.
The best approach for presence or length checks is a post-condition REPEAT UNTIL loop. This guarantees the prompt and input statements execute at least once before checking the condition at the end. We use the built-in string function LENGTH() to verify if the entered password has 8 or more characters, keeping the user stuck in the loop if it falls short.
9. Counting Vowels in a String
String manipulation and parsing are essential skills in data formatting and text analysis. This particular algorithm aims to step through an entire string, character by character, to count the total number of vowels present.
It achieves this by using a FOR loop bounded by the string's total length. Inside the loop, the MID() function extracts exactly one character at the current index sequence. Then, to keep the logic clean and avoid a massive, chained IF statement, it utilizes a CASE OF selection block. Converting the character to lowercase (LCASE()) ensures it catches both uppercase and lowercase vowels efficiently.
10. Simple Menu System
Menus form the core navigational backbone of console or text-based applications. Developing a robust menu requires continuously displaying options to the user, capturing their input, executing the corresponding action, and handling invalid choices gracefully.
This algorithm wraps everything inside a WHILE loop that continues indefinitely until the specific "exit" flag (option 3) is selected. Inside, a CASE OF block acts as the traffic controller, routing the execution based on the user's input. Crucially, the OTHERWISE clause acts as a fallback to gracefully handle any unexpected or invalid integers entered.
Ready to practice? Try our editor.
Give our pseudocode editor a go today for free - with a built-in compiler, tools to convert pseudocode to code, and project saving, PseudoEditor makes writing pseudocode easier than ever!
Start coding now