In this chapter, we will explore practical applications of Pascal. We will build a calculator, develop a simple game, and create a basic database application. These examples will demonstrate how to apply the concepts you have learned so far in real-world scenarios.
We will create a simple calculator that can perform basic arithmetic operations: addition, subtraction, multiplication, and division.
program Calculator;
uses
crt, SysUtils;
var
num1, num2, result: real;
operation: char;
begin
ClrScr;
writeln('Simple Calculator');
writeln('-----------------');
writeln('Enter first number: ');
readln(num1);
writeln('Enter an operation (+, -, *, /): ');
readln(operation);
writeln('Enter second number: ');
readln(num2);
case operation of
'+': result := num1 + num2;
'-': result := num1 - num2;
'*': result := num1 * num2;
'/': if num2 <> 0 then
result := num1 / num2
else
begin
writeln('Error: Division by zero');
exit;
end;
else
writeln('Error: Invalid operation');
exit;
end;
writeln('Result: ', result:0:2);
end.
case
statement to perform the appropriate arithmetic operation based on the user’s input.We will create a simple guessing game where the user has to guess a randomly generated number.
program GuessingGame;
uses
crt, SysUtils;
var
secretNumber, guess: integer;
attempts: integer;
begin
ClrScr;
Randomize;
secretNumber := Random(100) + 1; { Generate a random number between 1 and 100 }
attempts := 0;
writeln('Guess the Number Game');
writeln('---------------------');
repeat
writeln('Enter your guess (1-100): ');
readln(guess);
attempts := attempts + 1;
if guess < secretNumber then
writeln('Too low!')
else if guess > secretNumber then
writeln('Too high!')
else
writeln('Congratulations! You guessed the number in ', attempts, ' attempts.');
until guess = secretNumber;
end.
We will create a simple database application to store and manage student records. Each student record will include a name and an age.
program StudentDatabase;
uses
crt, SysUtils;
type
Student = record
name: string[50];
age: integer;
end;
var
students: array[1..100] of Student;
studentCount: integer;
procedure AddStudent(name: string; age: integer);
begin
if studentCount < 100 then
begin
studentCount := studentCount + 1;
students[studentCount].name := name;
students[studentCount].age := age;
end
else
writeln('Error: Student database is full.');
end;
procedure ListStudents;
var
i: integer;
begin
writeln('Student List:');
writeln('-------------');
for i := 1 to studentCount do
writeln(i, ': ', students[i].name, ', Age: ', students[i].age);
end;
var
choice: char;
name: string;
age: integer;
begin
ClrScr;
studentCount := 0;
repeat
writeln('Student Database');
writeln('----------------');
writeln('1. Add Student');
writeln('2. List Students');
writeln('3. Exit');
writeln('Enter your choice: ');
readln(choice);
case choice of
'1':
begin
writeln('Enter student name: ');
readln(name);
writeln('Enter student age: ');
readln(age);
AddStudent(name, age);
end;
'2': ListStudents;
'3': writeln('Exiting...');
else
writeln('Invalid choice.');
end;
writeln;
until choice = '3';
end.
AddStudent
procedure adds a new student to the array.ListStudents
procedure lists all the students in the database.