Algorithms
Algorithms
1. Start
2. Read first number (A)
3. Read second number (B)
4. Compute Sum = A + B
5. Display Sum
6. Stop
2. Flowcharts
A flowchart is a graphical representation of an algorithm using symbols to denote
different operations.
Co
Start
│
▼
Read A
│
▼
Read B
│
▼
Sum = A + B
│
▼
Display Sum
│
▼
Stop
Start
↓
Input Principal (P)
↓
Input Rate of Interest (R)
↓
Input Time (T)
↓
Calculate Simple Interest: SI = (P × R × T) / 100
↓
Display SI
↓
End
Differences Between Algorithm & Flowchart
Algorithm Flowchart
Text-based representation. Graphical representation.
Uses natural language or pseudocode. Uses symbols and arrows.
Easier to debug and analyze logic. Easier to visualize the flow of control.
No strict rules for writing. Follows standardized symbols.
Key Features:
Example in C:
#include <stdio.h>
int main() {
int num1 = 5, num2 = 10;
int sum = add(num1, num2);
printf("Sum: %d", sum);
return 0;
}
Explanation:
Key Features:
prolog
Comparison Table
Generation Example Languages Key Features Use Case
1GL Machine Code Binary, hardware-specific Early computers
2GL Assembly Mnemonics, assembler Firmware, drivers
Generation Example Languages Key Features Use Case
3GL C, Java, Python Portable, high-level syntax Modern software
4GL SQL, MATLAB Domain-specific, minimal code Databases, analytics
5GL Prolog, AI tools Natural language, AI-driven AI, expert systems
int main() {
int a = 5, b = 3;
return 0;
#include <iostream>
int main() {
int a, b;
return 0;
}
This is a simple C++ program that calculates the sum of two numbers and prints the
result. Below is a line-by-line breakdown:
1. #include <iostream>
• Purpose: Includes the Input/Output Stream library, which allows the program to
use functions like cin (input) and cout (output).
• Example: Without this, cout and cin won’t work.
4. int a = 5, b = 3;
• Purpose: Declares two integer variables (a and b) and assigns them values
(5 and 3).
• Key Points:
o int means integer (whole numbers).
o Variables can be modified later (e.g., a = 10;).
Possible Modifications
1. User Input Version (Dynamic Values):
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Sum: " << a + b;
return 0;
}
Now, the user can input any two numbers.
o
2. Using Functions (Modular Approach):
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 3;
cout << "Sum: " << add(a, b);
return 0;
}