Flowcharts Algorithms Examples

Introduction

Flowcharts and algorithms are essential tools for visualizing and designing solutions to computational problems. This topic provides practical examples that demonstrate how to create flowcharts and corresponding algorithms for common programming scenarios, helping bridge the gap between problem-solving concepts and actual code implementation.

Key Concepts

Flowchart: Visual representation of an algorithm using symbols and flowlines Algorithm: Step-by-step procedure to solve a problem Pseudocode: Structured English-like representation of algorithm logic Logic Flow: Sequential progression of steps in problem-solving

Basic Examples

Example 1: Find Maximum of Two Numbers

Algorithm:

  1. Start
  2. Input first number (A)
  3. Input second number (B)
  4. If A > B then Maximum = A, else Maximum = B
  5. Display Maximum
  6. Stop

Flowchart Symbols:

  • Oval: Start/Stop
  • Parallelogram: Input/Output
  • Rectangle: Process
  • Diamond: Decision
  • Arrow: Flow direction

C Implementation:

#include <stdio.h>
int main() {
    int a, b, max;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);
    if (a > b)
        max = a;
    else
        max = b;
    printf("Maximum: %d\n", max);
    return 0;
}

Example 2: Sum of First N Natural Numbers

Algorithm:

  1. Start
  2. Input N
  3. Initialize Sum = 0, Counter = 1
  4. While Counter <= N
    • Sum = Sum + Counter
    • Counter = Counter + 1
  5. Display Sum
  6. Stop

C Implementation:

#include <stdio.h>
int main() {
    int n, sum = 0, counter = 1;
    printf("Enter N: ");
    scanf("%d", &n);
    while (counter <= n) {
        sum += counter;
        counter++;
    }
    printf("Sum = %d\n", sum);
    return 0;
}

Control Structure Examples

Example 3: Grade Classification

Problem: Classify student grade based on marks

Algorithm:

  1. Start
  2. Input Marks
  3. If Marks >= 90 then Grade = “A” Else If Marks >= 80 then Grade = “B” Else If Marks >= 70 then Grade = “C” Else If Marks >= 60 then Grade = “D” Else Grade = “F”
  4. Display Grade
  5. Stop

Example 4: Factorial Calculation

Algorithm:

  1. Start
  2. Input N
  3. Initialize Factorial = 1
  4. For I = 1 to N
    • Factorial = Factorial * I
  5. Display Factorial
  6. Stop

C Implementation:

#include <stdio.h>
int main() {
    int n, factorial = 1;
    printf("Enter number: ");
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        factorial *= i;
    }
    printf("Factorial = %d\n", factorial);
    return 0;
}

Array Processing Examples

Example 5: Find Largest Element in Array

Algorithm:

  1. Start
  2. Input array size N
  3. Input array elements
  4. Initialize Largest = Array[0]
  5. For I = 1 to N-1
    • If Array[I] > Largest then Largest = Array[I]
  6. Display Largest
  7. Stop

Algorithm:

  1. Start
  2. Input array elements and search element
  3. Initialize Found = False, Position = -1
  4. For I = 0 to N-1
    • If Array[I] = Search_Element then
      • Found = True
      • Position = I
      • Break
  5. If Found then Display Position else Display “Not Found”
  6. Stop

Mathematical Examples

Example 7: Check Prime Number

Algorithm:

  1. Start
  2. Input Number
  3. If Number <= 1 then Display “Not Prime”
  4. Initialize IsPrime = True
  5. For I = 2 to √Number
    • If Number % I = 0 then
      • IsPrime = False
      • Break
  6. If IsPrime then Display “Prime” else Display “Not Prime”
  7. Stop

Example 8: Fibonacci Series

Algorithm:

  1. Start
  2. Input N (number of terms)
  3. Initialize First = 0, Second = 1
  4. Display First, Second
  5. For I = 3 to N
    • Next = First + Second
    • Display Next
    • First = Second
    • Second = Next
  6. Stop

Advanced Examples

Example 9: Bubble Sort

Algorithm:

  1. Start
  2. Input array elements
  3. For I = 0 to N-2
    • For J = 0 to N-2-I
      • If Array[J] > Array[J+1] then
        • Swap Array[J] and Array[J+1]
  4. Display sorted array
  5. Stop

Example 10: Menu-Driven Calculator

Algorithm:

  1. Start
  2. Display Menu (Add, Subtract, Multiply, Divide, Exit)
  3. Input Choice
  4. If Choice = 5 then Go to Step 8
  5. Input two numbers
  6. Perform operation based on choice
  7. Display result, Go to Step 2
  8. Stop

Problem-Solving Approach

Step-by-Step Methodology:

  1. Understand the Problem: Read and analyze requirements
  2. Identify Inputs/Outputs: Determine what data is needed
  3. Break Down the Problem: Divide into smaller sub-problems
  4. Design Algorithm: Write step-by-step solution
  5. Create Flowchart: Visualize the algorithm flow
  6. Test with Examples: Verify logic with sample data
  7. Implement in Code: Convert to programming language

Common Flowchart Patterns:

  • Sequential: Straight-line execution
  • Selection: If-else decision making
  • Iteration: Loop structures
  • Function Calls: Subroutine invocation

Best Practices

Algorithm Design:

  • Use clear, unambiguous language
  • Number each step for easy reference
  • Include all necessary inputs and outputs
  • Handle edge cases and error conditions

Flowchart Creation:

  • Use standard symbols consistently
  • Maintain proper flow direction (top to bottom, left to right)
  • Avoid crossing flow lines when possible
  • Include all decision paths
  • Use descriptive labels for processes

Code Implementation:

  • Follow algorithm steps directly
  • Add comments explaining complex logic
  • Use meaningful variable names
  • Test with various input scenarios

Common Mistakes to Avoid

  1. Infinite Loops: Ensure loop conditions eventually become false
  2. Missing Edge Cases: Consider boundary conditions
  3. Incorrect Logic Flow: Verify all decision paths
  4. Unclear Steps: Make each step specific and actionable
  5. Skipping Initialization: Initialize variables before use

Summary

Flowcharts and algorithms serve as blueprints for programming solutions. They help visualize problem-solving logic before coding, making implementation more systematic and error-free. The examples demonstrate progression from simple sequential problems to complex iterative and conditional scenarios, providing a foundation for structured programming approaches.


Part of BCA Programming with C Course (UGCOA22J201)