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:
- Start
- Input first number (A)
- Input second number (B)
- If A > B then Maximum = A, else Maximum = B
- Display Maximum
- 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:
- Start
- Input N
- Initialize Sum = 0, Counter = 1
- While Counter <= N
- Sum = Sum + Counter
- Counter = Counter + 1
- Display Sum
- 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:
- Start
- Input Marks
- 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”
- Display Grade
- Stop
Example 4: Factorial Calculation
Algorithm:
- Start
- Input N
- Initialize Factorial = 1
- For I = 1 to N
- Factorial = Factorial * I
- Display Factorial
- 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:
- Start
- Input array size N
- Input array elements
- Initialize Largest = Array[0]
- For I = 1 to N-1
- If Array[I] > Largest then Largest = Array[I]
- Display Largest
- Stop
Example 6: Linear Search
Algorithm:
- Start
- Input array elements and search element
- Initialize Found = False, Position = -1
- For I = 0 to N-1
- If Array[I] = Search_Element then
- Found = True
- Position = I
- Break
- If Array[I] = Search_Element then
- If Found then Display Position else Display “Not Found”
- Stop
Mathematical Examples
Example 7: Check Prime Number
Algorithm:
- Start
- Input Number
- If Number <= 1 then Display “Not Prime”
- Initialize IsPrime = True
- For I = 2 to √Number
- If Number % I = 0 then
- IsPrime = False
- Break
- If Number % I = 0 then
- If IsPrime then Display “Prime” else Display “Not Prime”
- Stop
Example 8: Fibonacci Series
Algorithm:
- Start
- Input N (number of terms)
- Initialize First = 0, Second = 1
- Display First, Second
- For I = 3 to N
- Next = First + Second
- Display Next
- First = Second
- Second = Next
- Stop
Advanced Examples
Example 9: Bubble Sort
Algorithm:
- Start
- Input array elements
- 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]
- If Array[J] > Array[J+1] then
- For J = 0 to N-2-I
- Display sorted array
- Stop
Example 10: Menu-Driven Calculator
Algorithm:
- Start
- Display Menu (Add, Subtract, Multiply, Divide, Exit)
- Input Choice
- If Choice = 5 then Go to Step 8
- Input two numbers
- Perform operation based on choice
- Display result, Go to Step 2
- Stop
Problem-Solving Approach
Step-by-Step Methodology:
- Understand the Problem: Read and analyze requirements
- Identify Inputs/Outputs: Determine what data is needed
- Break Down the Problem: Divide into smaller sub-problems
- Design Algorithm: Write step-by-step solution
- Create Flowchart: Visualize the algorithm flow
- Test with Examples: Verify logic with sample data
- 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
- Infinite Loops: Ensure loop conditions eventually become false
- Missing Edge Cases: Consider boundary conditions
- Incorrect Logic Flow: Verify all decision paths
- Unclear Steps: Make each step specific and actionable
- 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)