1. Problem Analysis (Competency Level 10.1)
Exercise 1.1: Input/Output Basics
- Description : Write a program that asks the user for their name and age, then prints a personalized greeting like:
“Hello [Name]! You are [Age] years old.” - Syllabus Coverage : Identification of inputs (name, age) and outputs (greeting).
- Aim : Teach students how to identify inputs and outputs and implement basic input/output operations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | program GreetingProgram; uses crt; (* Clear screen support *) var name: string; (* Input: User's name *) age: integer; (* Input: User's age *) begin clrscr; (* Clear screen before execution *) (* Ask for user input *) write('Enter your name: '); readln(name); write('Enter your age: '); readln(age); (* Output the personalized greeting *) writeln; writeln('Hello ', name, '! You are ', age, ' years old.'); readln; (* Wait for user to press Enter before closing *) end. |
Exercise 1.2: Area of a Rectangle
- Description : Write a program that calculates the area of a rectangle. The user should input the length and width, and the program should output the area.
- Syllabus Coverage : Problem analysis (inputs: length, width; output: area).
- Aim : Introduce students to simple mathematical computations and problem-solving.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | program RectangleArea; uses crt; (* Clear screen support *) var length, width, area: real; (* Variables for input and output *) begin clrscr; (* Clear screen before execution *) (* Ask for user input *) write('Enter the length of the rectangle: '); readln(length); write('Enter the width of the rectangle: '); readln(width); (* Compute the area *) area := length * width; (* Output the result *) writeln; writeln('The area of the rectangle is: ', area:0:2); readln; (* Wait for user to press Enter before closing *) end. |
2. Control Structures (Competency Levels 10.2, 10.6, 10.7)
Selection (IF-Else, Case/Switch)
Exercise 2.1: Positive, Negative, or Zero
- Description : Write a program that checks if a number entered by the user is positive, negative, or zero. Use
if-else
statements. - Syllabus Coverage : Selection control structure.
- Aim : Teach students how to use conditional statements to make decisions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | program CheckNumber; uses crt; (* Clear screen support *) var num: integer; (* Variable to store the user input *) begin clrscr; (* Clear screen before execution *) (* Ask for user input *) write('Enter a number: '); readln(num); (* Decision-making using if-else *) if num > 0 then writeln('The number is positive.') else if num < 0 then writeln('The number is negative.') else writeln('The number is zero.'); readln; (* Wait for user to press Enter before closing *) end. |
Exercise 2.2: Simple Calculator
- Description : Create a simple calculator where the user can choose an operation (
+
,-
,*
,/
) using acase
statement. The program should perform the selected operation on two numbers provided by the user. - Syllabus Coverage : Selection control structure with multiple conditions.
- Aim : Reinforce the use of
case
statements and arithmetic operators.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | program SimpleCalculator; uses crt; // Optional: For clearing the screen and waiting for key press var num1, num2, result: Real; // Variables to store numbers and result choice: Char; // Variable to store the user's operation choice begin clrscr; // Clears the screen (optional) // Prompt the user to enter two numbers writeln('Enter the first number:'); readln(num1); writeln('Enter the second number:'); readln(num2); // Display the available operations writeln('Choose an operation:'); writeln('+ : Addition'); writeln('- : Subtraction'); writeln('* : Multiplication'); writeln('/ : Division'); // Read the user's choice writeln('Enter your choice (+, -, *, /):'); readln(choice); // Perform the selected operation using a case statement case choice of '+': begin result := num1 + num2; writeln('Result: ', num1:0:2, ' + ', num2:0:2, ' = ', result:0:2); end; '-': begin result := num1 - num2; writeln('Result: ', num1:0:2, ' - ', num2:0:2, ' = ', result:0:2); end; '*': begin result := num1 * num2; writeln('Result: ', num1:0:2, ' * ', num2:0:2, ' = ', result:0:2); end; '/': begin if num2 <> 0 then begin result := num1 / num2; writeln('Result: ', num1:0:2, ' / ', num2:0:2, ' = ', result:0:2); end else writeln('Error: Division by zero is not allowed.'); end; else writeln('Invalid choice! Please select a valid operation.'); end; readkey; // Waits for a key press before closing (optional) end. |
Iteration (Loops)
Exercise 2.3: Even Numbers from 1 to 50
- Description : Write a program that prints all even numbers from 1 to 50 using a
for
loop. - Syllabus Coverage : Iteration control structure.
- Aim : Introduce students to loops and repetitive tasks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | program PrintEvenNumbers; uses crt; // Optional: For clearing the screen and waiting for key press var i: Integer; // Loop counter variable begin clrscr; // Clears the screen (optional) writeln('Even numbers from 1 to 50:'); // Use a for loop to iterate through numbers from 1 to 50 for i := 1 to 50 do begin // Check if the number is even if i mod 2 = 0 then writeln(i); // Print the even number end; |
Exercise 2.4: Factorial of a Number
- Description : Write a program that calculates the factorial of a number entered by the user using a
while
loop. - Syllabus Coverage : Iteration control structure with unknown iterations.
- Aim : Teach students how to use loops for mathematical computations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | program FactorialCalculation; uses crt; // Optional: For clearing the screen and waiting for key press var Number, Factorial, i: Integer; // Variables to store the number, factorial result, and loop counter begin clrscr; // Clears the screen (optional) // Prompt the user to enter a number writeln('Enter a positive integer to calculate its factorial:'); readln(Number); // Initialize variables Factorial := 1; // Factorial starts at 1 i := 1; // Loop counter starts at 1 // Use a while loop to calculate the factorial while i <= Number do begin Factorial := Factorial * i; // Multiply the current factorial by i i := i + 1; // Increment the loop counter end; // Output the result writeln('The factorial of ', Number, ' is: ', Factorial); readkey; // Waits for a key press before closing (optional) end. |
Exercise 2.5: Guess the Secret Number
- Description : Write a program that asks the user to guess a secret number (e.g., 7). The program should keep asking until the user guesses correctly, using a
repeat-until
loop. - Syllabus Coverage : Iteration control structure with condition checking at the end.
- Aim : Reinforce the use of loops and introduce game-like programming.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | program GuessTheNumber; uses crt; // Optional: For clearing the screen and waiting for key press var Guess, SecretNumber: Integer; // Variables to store the user's guess and the secret number begin clrscr; // Clears the screen (optional) SecretNumber := 7; // The secret number to be guessed writeln('Welcome to the Guess the Number Game!'); writeln('I am thinking of a number between 1 and 10.'); writeln('Can you guess what it is?'); // Use a repeat-until loop to keep asking until the user guesses correctly repeat write('Enter your guess: '); readln(Guess); // Read the user's guess if Guess <> SecretNumber then writeln('Wrong guess! Try again.') else writeln('Congratulations! You guessed the correct number.'); until Guess = SecretNumber; // Continue looping until the guess matches the secret number readkey; // Waits for a key press before closing (optional) end. |
3. Data Types and Operators (Competency Levels 10.4, 10.5)
Exercise 3.1: Swap Two Variables
- Description : Write a program that swaps the values of two variables without using a third variable. Use arithmetic operators.
- Syllabus Coverage : Variables, data types, and arithmetic operators.
- Aim : Teach students how to manipulate variables and use operators effectively.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | program SwapWithoutThirdVariable; uses crt; var a, b: integer; begin clrscr; (* Input values from user *) write('Enter first number (a): '); readln(a); write('Enter second number (b): '); readln(b); (* Display values before swapping *) writeln('Before swapping: a = ', a, ', b = ', b); (* Swap values using arithmetic operations *) a := a + b; b := a - b; a := a - b; (* Display values after swapping *) writeln('After swapping: a = ', a, ', b = ', b); readln; end. |
Exercise 3.2: Temperature Converter
- Description : Write a program that converts temperature from Celsius to Fahrenheit and vice versa. Allow the user to choose the conversion type using logical operators.
- Syllabus Coverage : Logical and arithmetic operators.
- Aim : Reinforce the use of operators and decision-making.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | program TemperatureConverter; uses crt; // Optional: For clearing the screen and waiting for key press var choice: Char; // Variable to store the user's choice celsius, fahrenheit: Real; // Variables to store temperatures begin clrscr; // Clears the screen (optional) writeln('Temperature Conversion Program'); writeln('Choose the type of conversion:'); writeln('1. Celsius to Fahrenheit (Enter C)'); writeln('2. Fahrenheit to Celsius (Enter F)'); write('Enter your choice (C/F): '); readln(choice); // Use logical operators to decide the conversion type if (choice = 'C') or (choice = 'c') then begin writeln('Enter the temperature in Celsius:'); readln(celsius); // Convert Celsius to Fahrenheit using the formula: F = (C * 9/5) + 32 fahrenheit := (celsius * 9 / 5) + 32; writeln('The temperature in Fahrenheit is: ', fahrenheit:0:2); end else if (choice = 'F') or (choice = 'f') then begin writeln('Enter the temperature in Fahrenheit:'); readln(fahrenheit); // Convert Fahrenheit to Celsius using the formula: C = (F - 32) * 5/9 celsius := (fahrenheit - 32) * 5 / 9; writeln('The temperature in Celsius is: ', celsius:0:2); end else begin writeln('Invalid choice! Please enter either C or F.'); end; readkey; // Waits for a key press before closing (optional) end. |
Exercise 3.3: Leap Year Checker
- Description : Write a program that determines whether a year entered by the user is a leap year. A leap year is divisible by 4 but not by 100, unless it is also divisible by 400.
- Syllabus Coverage : Logical and comparison operators.
- Aim : Teach students how to apply logical conditions to solve real-world problems.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | program LeapYearChecker; uses crt; // Optional: For clearing the screen and waiting for key press var year: Integer; // Variable to store the year entered by the user begin clrscr; // Clears the screen (optional) writeln('Leap Year Checker'); writeln('Enter a year to check if it is a leap year:'); readln(year); // Read the year from the user // Use logical and comparison operators to determine if the year is a leap year if (year mod 400 = 0) or ((year mod 4 = 0) and (year mod 100 <> 0)) then writeln(year, ' is a leap year.') else writeln(year, ' is not a leap year.'); readkey; // Waits for a key press before closing (optional) end. |
4. Arrays (Competency Level 10.9)
Exercise 4.1: Reverse Array
- Description : Write a program that stores 5 student names in an array and prints them in reverse order.
- Syllabus Coverage : One-dimensional arrays.
- Aim : Introduce students to arrays and their basic operations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | program ReverseStudentNames; uses crt; var students: array[1..5] of string; i: integer; begin clrscr; writeln('Enter 5 student names:'); (* Input student names *) for i := 1 to 5 do begin write('Student ', i, ': '); readln(students[i]); end; (* Print student names in reverse order *) writeln('Student names in reverse order:'); for i := 5 downto 1 do writeln(students[i]); readln; end. |
Exercise 4.2: Largest and Smallest Numbers
- Description : Write a program that finds the largest and smallest numbers in an array of integers.
- Syllabus Coverage : Array operations (accessing and comparing values).
- Aim : Reinforce the use of arrays and teach students how to process data stored in arrays.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | program FindMinMax; uses crt; const Size = 5; var numbers: array[1..Size] of integer; i, min, max: integer; begin clrscr; writeln('Enter ', Size, ' integers:'); (* Input numbers into the array *) for i := 1 to Size do begin write('Number ', i, ': '); readln(numbers[i]); end; (* Initialize min and max with the first element *) min := numbers[1]; max := numbers[1]; (* Find min and max values *) for i := 2 to Size do begin if numbers[i] < min then min := numbers[i]; if numbers[i] > max then max := numbers[i]; end; (* Display results *) writeln('Smallest number: ', min); writeln('Largest number: ', max); readln; end. |
Exercise 4.3: Average of Numbers
- Description : Write a program that calculates the average of numbers stored in an array. Allow the user to input the numbers.
- Syllabus Coverage : Array operations and arithmetic computations.
- Aim : Combine arrays with mathematical operations to solve practical problems.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | program CalculateAverage; uses crt; const Size = 5; var numbers: array[1..Size] of integer; i, sum: integer; average: real; begin clrscr; writeln('Enter ', Size, ' numbers:'); (* Input numbers into the array *) sum := 0; for i := 1 to Size do begin write('Number ', i, ': '); readln(numbers[i]); sum := sum + numbers[i]; end; (* Calculate average *) average := sum / Size; (* Display result *) writeln('The average of the entered numbers is: ', average:0:2); readln; end. |
5. Sub-Programs (Competency Level 10.10)
Exercise 5.1: Square Function
- Description : Write a program with a function that calculates the square of a number. Call this function from the main program to calculate the squares of multiple numbers.
- Syllabus Coverage : Value-returning sub-programs.
- Aim : Teach students how to modularize code using functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | program CalculateSquare; uses crt; (* Function to calculate square of a number *) function Square(num: integer): integer; begin Square := num * num; end; const Size = 5; var numbers: array[1..Size] of integer; i: integer; begin clrscr; writeln('Enter ', Size, ' numbers:'); (* Input numbers into the array *) for i := 1 to Size do begin write('Number ', i, ': '); readln(numbers[i]); end; (* Calculate and display squares *) writeln('Squares of the entered numbers:'); for i := 1 to Size do writeln('Square of ', numbers[i], ' is ', Square(numbers[i])); readln; end. |
Exercise 5.2: Multiplication Table Procedure
- Description : Write a program with a procedure that prints a multiplication table for a given number. Call this procedure from the main program.
- Syllabus Coverage : Non-value-returning sub-programs.
- Aim : Reinforce the use of procedures for reusable code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | program MultiplicationTable; uses crt; (* Procedure to print multiplication table of a number *) procedure PrintTable(num: integer); var i: integer; begin writeln('Multiplication table for ', num, ':'); for i := 1 to 10 do writeln(num, ' x ', i, ' = ', num * i); end; var number: integer; begin clrscr; (* Get input number *) write('Enter a number to print its multiplication table: '); readln(number); (* Call procedure to print table *) PrintTable(number); readln; end. |
Exercise 5.3: Prime Number Checker
- Description : Write a program with a function that checks if a number is prime. Use this function to print all prime numbers between 1 and 100.
- Syllabus Coverage : Combining functions with loops.
- Aim : Teach students how to combine sub-programs with other control structures.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | program PrimeNumbers; uses crt; (* Function to check if a number is prime *) function IsPrime(num: integer): boolean; var i: integer; begin if (num < 2) then IsPrime := false else begin IsPrime := true; for i := 2 to trunc(sqrt(num)) do begin if (num mod i = 0) then begin IsPrime := false; break; end; end; end; end; var i: integer; begin clrscr; writeln('Prime numbers between 1 and 100:'); for i := 1 to 100 do begin if IsPrime(i) then write(i, ' '); end; writeln; readln; end. |