Programming Logic
Understanding Algorithms, Pseudocode and Flowcharts
1. What is Programming Logic?
Before writing a program in C, Java, Python or another programming language, it is useful to first think about the solution.
In simple words:
Logic Building Tools
| Tool | Purpose |
|---|---|
| Algorithm | Step-by-step solution written in simple human language. |
| Pseudocode | Algorithm written using simple, programming-like notation. |
| Flowchart | Graphical representation of the steps of a solution. |
2. Algorithm
Characteristics of a Good Algorithm
- It should have a clear beginning and ending.
- Steps should be arranged in the correct order.
- Each step should be clear and unambiguous.
- It should eventually produce the required result.
- It should be easy to convert into a programming language.
Example: Store and Display a Number
Problem: Write an algorithm to store the number 25 and display it.
Algorithm – Detailed Version
- Start the program.
- Reserve some memory space to store a number.
- Store the value
25in the allocated memory. - Display the value stored in the memory.
- Stop the program.
Algorithm Using a Variable Name
- Start the program.
- Reserve memory to store a number and give the memory location the name
a. - Store the value
25ina. - Display the value stored in
a. - Stop the program.
a to a memory location
makes the algorithm easier to understand and later convert into code.
3. Pseudocode
Pseudocode is sometimes called “false code” because it looks similar to programming code but does not follow the exact syntax of a particular programming language.
Algorithm vs Pseudocode
| Algorithm | Pseudocode |
|---|---|
| Written mainly in natural language. | Uses programming-like statements. |
| Easy for beginners to understand. | Closer to actual program structure. |
| Does not depend on programming syntax. | Also generally language-independent. |
Pseudocode for Storing and Displaying a Number
START
DECLARE a
a := 25
WRITE "Value of a = ", a
STOP
:= are commonly used in pseudocode
to represent assignment. Pseudocode conventions can vary.
4. From Pseudocode to a Real Program
The same logic can be implemented in different programming languages. The algorithm remains essentially the same, but the syntax changes.
C Language
#include <stdio.h>
int main()
{
int a;
a = 25;
printf("a = %d", a);
return 0;
}
Java
public class Sample
{
public static void main(String[] args)
{
int a;
a = 25;
System.out.println("a = " + a);
}
}
Python
a = 25
print("a =", a)
5. Example 1 – Sum of Two Numbers
Problem: Calculate and display the sum of any two numbers.
Algorithm
- Start.
- Declare variables
a,bandsum. - Read the values of
aandb. - Calculate
sum = a + b. - Display
sum. - Stop.
Pseudocode
START
DECLARE a, b, sum
READ a
READ b
sum := a + b
WRITE "Sum = ", sum
STOP
6. Example 2 – Area of a Rectangle
Problem: Calculate and display the area of a rectangle for a given length and breadth.
Algorithm
- Start.
- Declare
length,breadthandarea. - Read the length.
- Read the breadth.
- Calculate
area = length × breadth. - Display the area.
- Stop.
Pseudocode
START
DECLARE length, breadth, area
READ length
READ breadth
area := length * breadth
WRITE "Area = ", area
STOP
7. Example 3 – Area of a Circle
Problem: Calculate and display the area of a circle for a given radius.
For beginner-level calculations, we can use
π = 3.14.
Algorithm
- Start.
- Declare
radiusandarea. - Read the radius.
- Calculate
area = 3.14 × radius × radius. - Display the area.
- Stop.
Pseudocode
START
DECLARE radius, area
READ radius
area := 3.14 * radius * radius
WRITE "Area of circle = ", area
STOP
8. Example 4 – Average of Three Numbers
Problem: Calculate and display the average of three numbers.
Algorithm
- Start.
- Declare
a,b,candavg. - Read the three numbers.
- Calculate the average.
- Display the average.
- Stop.
Pseudocode
START
DECLARE a, b, c, avg
READ a
READ b
READ c
avg := (a + b + c) / 3
WRITE "Average = ", avg
STOP
9. Example 5 – Simple Interest
Problem: Calculate Simple Interest using principal, rate of interest and duration.
Where:
- P = Principal amount
- R = Rate of interest
- N = Duration / Number of years
- SI = Simple Interest
Algorithm
- Start.
- Declare
p,r,nandsi. - Read the principal amount.
- Read the rate of interest.
- Read the duration.
- Calculate
si = (p × r × n) / 100. - Display the simple interest.
- Stop.
Pseudocode
START
DECLARE p, r, n, si
READ p
READ r
READ n
si := (p * r * n) / 100
WRITE "Simple Interest = ", si
STOP
10. Flowchart
Common Flowchart Symbols
| Symbol / Shape | Name | Purpose |
|---|---|---|
| Oval | Terminator | Represents Start or Stop. |
| Rectangle | Process | Represents a calculation or processing step. |
| Parallelogram | Input / Output | Represents reading input or displaying output. |
| Diamond | Decision | Represents a condition or decision. |
| Arrow | Flow line | Shows the direction of execution. |
Simple Flow of a Program
11. How to Build Programming Logic
When you receive a programming problem, do not immediately start writing code. First break the problem into smaller steps.
Recommended Process
-
Understand the problem
Identify exactly what the problem is asking. -
Identify inputs
Determine what information the user must provide. -
Identify processing
Determine what calculations or decisions are required. -
Identify outputs
Determine what the program must display. -
Write the algorithm
Express the solution step by step. -
Write pseudocode
Convert the algorithm into programming-like instructions. -
Write the actual program
Convert the pseudocode into C, Java, Python, etc. -
Test the solution
Try different input values and verify the output.
Input → Process → Output
12. Practice Questions
Level 1 – Basic Understanding
Level 2 – Write Algorithms
Level 3 – Write Pseudocode
Level 4 – Logic Building
Level 5 – Flowchart Practice
13. Challenge Problems
14. Quick Revision
| Concept | Remember |
|---|---|
| Programming Logic | Deciding what instructions the computer should execute. |
| Algorithm | Step-by-step solution in simple language. |
| Pseudocode | Algorithm written in programming-like notation. |
| Flowchart | Graphical representation of an algorithm. |
| Input | Data supplied to the program. |
| Process | Calculation or operation performed on the input. |
| Output | Result produced by the program. |
Understand the problem → Identify Input → Identify Process → Identify Output → Write Algorithm → Write Pseudocode → Draw Flowchart → Write Program → Test.