BIT2201-BIT1210 Computer Programming Methodology Notes
BIT2201-BIT1210 Computer Programming Methodology Notes
1. Programming Concepts
Programming involves writing instructions for a computer to perform tasks. Key concepts
include:
2. Programming Languages
Programming languages are classified into different types:
Programming in C – May/Aug 2025
a) Low-Level Languages
Machine Language: Binary code (0s and 1s) directly understood by the CPU.
Assembly Language: Uses symbolic representations of machine instructions.
b) High-Level Languages
1. Start
2. Read length and width
3. Compute area = length * width
4. Print area
5. Stop
Start
|
[Input length, width]
|
[area = length * width]
|
[Print area]
|
Stop
Programming in C – May/Aug 2025
Class Tasks
Task 1
Task 2
Task 3
Write an algorithm and draw a flowchart for a program that converts temperature from Celsius to
Fahrenheit.
Assignments
Assignment 1
Write a simple C program that takes user input for two numbers and prints their sum.
Assignment 2
Conclusion
Understanding programming concepts, language types, and the development process is crucial
for writing efficient programs. A structured approach ensures that programs are clear, efficient,
and maintainable.
1. Understand program design tools such as algorithms, flowcharts, and decision trees.
2. Identify the features of a well-designed program.
3. Recognize different types of program errors and their causes.
a) Algorithms
1. Start
2. Declare variables a, b, and sum.
3. Read values of a and b.
4. Compute sum = a + b.
5. Print sum.
6. Stop
b) Flowcharts
Symbol Meaning
Oval Start/Stop
Parallelogra Input/Output
m
Rectangle Process (Calculation)
Diamond Decision
Start
|
[Input a, b]
|
[sum = a + b]
|
Programming in C – May/Aug 2025
[Print sum]
|
Stop
c) Decision Trees
A decision tree is a diagram that represents different choices and their possible outcomes.
Start
|
Score >= 50?
/ \
Yes No
"Pass" "Fail"
a) Syntax Errors
int main() {
printf("Hello, World!\n") // Missing semicolon
return 0;
}
b) Logical Errors
int main() {
int a = 5, b = 2;
printf("Sum: %d\n", a - b); // Wrong operator used
return 0;
}
c) Runtime Errors
int main() {
int x = 10, y = 0;
printf("Result: %d\n", x / y); // Division by zero error
return 0;
}
Class Tasks
Task 1
Write an algorithm and draw a flowchart for finding the largest of three numbers.
Task 2
Create a simple C program that demonstrates syntax, logical, and runtime errors, then fix them.
Task 3
Assignments
Assignment 1
Write a C program to calculate the factorial of a number using a decision tree approach.
Assignment 2
Conclusion
Program design tools such as algorithms, flowcharts, and decision trees help in writing structured
programs. A good program should be clear, efficient, modular, and robust. Understanding
syntax, logical, and runtime errors is essential for debugging and writing reliable programs.
1. Layout of a C Program
A C program follows a structured layout that consists of different sections:
// Function Declaration
int main() {
// Variable Declaration
int a;
// Output
printf("You entered: %d\n", a);
Example:
#include <stdio.h>
int main() {
int age = 25;
float height = 5.9;
char grade = 'A';
printf("Age: %d, Height: %.1f, Grade: %c\n", age, height, grade);
return 0;
}
3. Constants
Constants are fixed values that do not change during program execution.
Example:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("You entered: %d\n", num);
return 0;
}
int sum = a + b;
b) Statements
printf("Hello, World!\n");
Class Tasks
Task 1
Write a C program that declares an int, float, and char variable, assigns values, and prints
them.
Task 2
Write a program that takes two numbers as input and prints their sum.
Assignments
Assignment 1
Write a C program that takes user input for name, age, and grade, then prints them using
printf().
Assignment 2
Conclusion
Understanding the structure of a C program, data types, input/output operations, and expressions
is fundamental to writing efficient C programs. Mastery of these concepts will enable the
development of more complex applications.
Assignment- Key
Definition of Terms
Operator: A symbol that performs an operation on one or more operands.
Operand: A value or variable on which an operator acts.
Type Conversion (Casting): The process of converting one data type to another.
Comment: A line of code ignored by the compiler, used to describe or explain code.
Reserved Words: Predefined keywords in C that cannot be used as identifiers.
a) Arithmetic Operators
Example:
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("Sum: %d\n", a + b);
Programming in C – May/Aug 2025
printf("Difference: %d\n", a - b);
printf("Product: %d\n", a * b);
printf("Quotient: %d\n", a / b);
printf("Remainder: %d\n", a % b);
return 0;
}
Example:
if (a > b) {
printf("a is greater than b\n");
}
c) Logical Operators
Example:
d) Assignment Operators
Example:
int a = 10;
a += 5; // Equivalent to a = a + 5;
e) Bitwise Operators
Example:
int a = 10;
double b = a; // a is automatically converted to double
int a = 10, b = 3;
double result = (double)a / b;
printf("Result: %.2f\n", result);
Programming in C – May/Aug 2025
3. Comments in C
Comments are ignored by the compiler and help document the code.
a) Single-line comment
// This is a single-line comment
printf("Hello, World!\n");
b) Multi-line comment
/* This is a
multi-line comment */
printf("Hello, World!\n");
Example:
int main() {
int number = 10; // 'int' is a reserved word
return 0;
}
Class Tasks
Task 1
Addition
Programming in C – May/Aug 2025
Subtraction
Multiplication
Division
Modulus
Task 2
Task 3
Write a program that prints a message using both single-line and multi-line comments.
Assignments
Assignment 1
Write a program that takes two numbers as input and uses logical operators to:
Assignment 2
Write a program that prints a table of 10 reserved words and their meanings.
Conclusion
Understanding operators, operands, type conversion, comments, and reserved words is essential
for writing efficient C programs. Mastering these basics will help in building more complex
applications.
Programming in C – May/Aug 2025
Week 5: Control Structures
Objectives
By the end of this lesson, students should be able to:
Definition of Terms
Control Structures: Constructs that dictate the flow of execution in a program.
Sequential Execution: Statements are executed one after another in the order they
appear.
Looping (Iteration): Repeating a block of code multiple times.
Selection (Decision Making): Choosing between multiple execution paths based on
conditions.
1. Sequential Execution
Sequential execution follows a top-to-bottom approach. Example:
#include <stdio.h>
int main() {
printf("Hello, world!\n");
printf("This is sequential execution.\n");
return 0;
}
Explanation:
2. Loop Structures
Programming in C – May/Aug 2025
Loops allow repeated execution of a block of code.
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("Iteration %d\n", i);
}
return 0;
}
Explanation:
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("Iteration %d\n", i);
i++;
}
return 0;
}
Explanation:
#include <stdio.h>
Programming in C – May/Aug 2025
int main() {
int i = 1;
do {
printf("Iteration %d\n", i);
i++;
} while (i <= 5);
return 0;
}
Explanation:
The block runs at least once before checking while (i <= 5).
3. Selection Structures
a) if Statement
if (x > 0) {
printf("x is positive\n");
}
b) if-else Statement
if (x > 0) {
printf("x is positive\n");
} else {
printf("x is not positive\n");
}
c) Nested if-else
if (x > 0) {
printf("Positive\n");
} else if (x == 0) {
printf("Zero\n");
} else {
printf("Negative\n");
}
d) switch Statement
Explanation:
Class Tasks
Task 1
1. for loop
2. while loop
3. do-while loop
Task 2
Task 3
Write a program that takes a day number (1-7) as input and prints the corresponding day using
switch.
Assignments
Assignment 1
Write a program that prints the sum of even numbers from 1 to 100 using a loop.
Programming in C – May/Aug 2025
Assignment 2
Write a program that asks the user for their age and:
Conclusion
Control structures help direct program execution logically. Understanding loops and decision-
making statements is crucial for writing efficient C programs.
Week 7: Functions in C
Learning Objectives
By the end of this lesson, students should be able to:
Definition of Terms
1. Function: A block of code that performs a specific task and can be reused multiple times
in a program.
2. System Functions: Predefined functions provided by C, such as printf(), scanf(),
sqrt().
3. User-Defined Functions: Functions created by the programmer to perform specific
tasks.
Programming in C – May/Aug 2025
4. Function Prototype (Declaration): A statement that tells the compiler about a function’s
name, return type, and parameters before its actual definition.
5. Function Definition (Implementation): The actual code block of a function where the
logic is written.
6. Function Call: A statement used to execute a function in a program.
int main() {
double num = 16.0;
double result = sqrt(num);
printf("Square root of %.2f is %.2f", num, result);
return 0;
}
// Function Prototype
int addNumbers(int a, int b);
int main() {
int sum = addNumbers(5, 7);
printf("Sum: %d", sum);
return 0;
}
// Function Definition
int addNumbers(int a, int b) {
return a + b;
}
Function Call
To use a function, we call it within main() or another function. Example:
// Function Prototype
t long factorial(int n);
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d is %ld", num, factorial(num));
return 0;
}
// Function Definition
long factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
Breakdown:
1. Function Prototype: long factorial(int n); tells the compiler that factorial is a
function that takes an integer and returns a long integer.
2. User Input: The program takes an integer from the user.
3. Function Call: factorial(num) is called inside printf().
4. Recursive Function: The function calls itself with n-1 until n == 0.
Class Tasks
Programming in C – May/Aug 2025
Task 1: Write a function square() that returns the square of a number entered by
the user.
Task 2: Create a function isEven() that takes an integer and returns 1 if even and
0 if odd.
Task 3: Write a function gcd() to compute the greatest common divisor of two
numbers.
Assignments
Assignment 1:
Write a program that includes a function power() to calculate x^y (x raised to the power y),
where both x and y are user inputs.
Assignment 2:
Create a program with a function reverseDigits() that takes an integer and returns the
reversed number.
These tasks and assignments will help solidify your understanding of functions in C.
Argument Passing
Functions in C can receive arguments in two ways:
1. Pass by Value: A copy of the argument is passed to the function. Changes made inside
the function do not affect the original variable.
2. Pass by Reference: The function receives a reference (memory address) of the variable,
allowing modifications to reflect outside the function.
void modifyValue(int x) {
x = x * 2;
printf("Inside function: x = %d\n", x);
}
int main() {
int num = 5;
modifyValue(num);
printf("Outside function: num = %d\n", num);
return 0;
}
int main() {
int num = 5;
modifyValue(&num);
printf("After function call: num = %d\n", num);
return 0;
}
Output: The original value is modified because the function works on its memory address.
Programming in C – May/Aug 2025
Default Values in Function Arguments
C does not support default values for function arguments like C++, but this can be simulated
using function overloading or by providing optional parameters in another way.
Example:
#include <stdio.h>
int main() {
greet(NULL); // Uses default value
greet("Alice");
return 0;
}
Function Overloading
C does not support function overloading directly. Instead, different function names or function
pointers can be used to achieve similar behavior.
int main() {
printf("Sum (int): %d\n", addInt(5, 7));
printf("Sum (double): %.2f\n", addDouble(5.5, 7.5));
return 0;
}
Inline Functions
Programming in C – May/Aug 2025
C does not support inline functions like C++, but macros can achieve a similar effect by
replacing function calls with their definitions.
Macro Alternative in C:
#include <stdio.h>
#define square(x) ((x) * (x))
int main() {
printf("Square of 5: %d\n", square(5));
return 0;
}
Recursive Functions
A recursive function is a function that calls itself to solve a problem.
long factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
int num = 5;
printf("Factorial of %d is %ld\n", num, factorial(num));
return 0;
}
int fibonacci(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n = 10;
printf("Fibonacci series up to %d terms:\n", n);
for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
printf("\n");
return 0;
}
Programming in C – May/Aug 2025
Class Tasks
Task 1: Implement a function that swaps two numbers using pass-by-value and
pass-by-reference.
Task 2: Write a function that takes three numbers and returns the maximum,
using default arguments where applicable.
Assignments
Assignment 1:
Assignment 2:
Write a recursive function to generate and print the first n numbers of the Fibonacci series.
These tasks and assignments will help deepen your understanding of advanced function concepts
in Programming in C.
Concept of Arrays
An array is a collection of variables of the same type stored in contiguous memory locations.
Arrays allow multiple values to be handled using a single variable name.
Example of an Array:
int numbers[5] = {10, 20, 30, 40, 50};
datatype array_name[size];
Example:
int marks[10]; // Declares an integer array with 10 elements
Example:
#include <stdio.h>
int main() {
int numbers[3] = {10, 20, 30};
printf("First element: %d\n", numbers[0]);
printf("Second element: %d\n", numbers[1]);
return 0;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int sum = 0;
int size = sizeof(arr) / sizeof(arr[0]);
int main() {
int arr[] = {10, 20, 30, 40, 50};
int key = 30;
int found = 0;
int size = sizeof(arr) / sizeof(arr[0]);
if (!found) {
printf("Element %d not found in array\n", key);
Programming in C – May/Aug 2025
}
return 0;
}
Class Tasks
Task 1: Write a C program to find the largest element in an array.
Assignments
Assignment 1:
Write a program to sort an array in ascending order using the Bubble Sort algorithm.
Assignment 2:
These tasks and assignments will help deepen your understanding of Arrays in C.
Example of a Pointer:
int a = 10;
int *p = &a; // Pointer stores the address of variable 'a'
datatype *pointer_name;
Example:
int *ptr; // Declares a pointer to an integer
Example:
#include <stdio.h>
int main() {
int num = 10;
int *ptr = #
return 0;
}
Explanation:
Class Tasks
Task 1: Write a C program to print the address of a variable using a pointer.
Task 3: Write a program that uses a pointer to modify the value of a variable.
Assignments
Assignment 1:
Write a program that demonstrates pointer arithmetic by iterating over an integer array using
pointers.
Assignment 2:
Implement a program that uses pointers to find the largest and smallest elements in an array.
These tasks and assignments will help deepen your understanding of Pointers in C.
Example:
#include <stdio.h>
int main() {
FILE *file;
file = fopen("example.txt", "w");
fprintf(file, "Hello, File Processing in C!\n");
fclose(file);
return 0;
}
int main() {
FILE *file = fopen("data.txt", "w");
fprintf(file, "This is a sequential file.\n");
fclose(file);
return 0;
}
int main() {
FILE *file = fopen("data.txt", "r");
char text[100];
fgets(text, 100, file);
printf("%s", text);
fclose(file);
return 0;
}
Programming in C – May/Aug 2025
Random Access Files
Creating and Writing to a Random Access File
#include <stdio.h>
int main() {
FILE *file = fopen("random.dat", "wb");
int data = 42;
fwrite(&data, sizeof(int), 1, file);
fclose(file);
return 0;
}
int main() {
FILE *file = fopen("random.dat", "rb");
int data;
fread(&data, sizeof(int), 1, file);
printf("Read value: %d\n", data);
fclose(file);
return 0;
}
Assignments
1. Write a C program to count the number of words in a file.
2. Implement a program that appends text to an existing file and reads the contents.