0% found this document useful (0 votes)
5 views

Pascal-Syntax-Overview

The document provides an overview of Pascal syntax, including program structure, comments, variables, constants, input/output methods, arithmetic operators, control structures, and procedures/functions. It also contains 20 exercises with detailed explanations and solutions, covering fundamental programming concepts such as loops, conditionals, and mathematical calculations. Each exercise demonstrates practical applications of Pascal syntax through simple programs.

Uploaded by

djellouliakram3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Pascal-Syntax-Overview

The document provides an overview of Pascal syntax, including program structure, comments, variables, constants, input/output methods, arithmetic operators, control structures, and procedures/functions. It also contains 20 exercises with detailed explanations and solutions, covering fundamental programming concepts such as loops, conditionals, and mathematical calculations. Each exercise demonstrates practical applications of Pascal syntax through simple programs.

Uploaded by

djellouliakram3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Pascal Syntax Overview

1. Program Structure

program HelloWorld;
begin
writeln('Hello, world!');
end.

2. Comments

{ This is a comment }
(* Another comment style *)

3. Variables

var
age: Integer;
name: String;
price: Real;
isAlive: Boolean;

4. Constants

const
PI = 3.14;
MAX = 100;

5. Input/Output

read(variable); { Input }
readln(variable); { Input + newline }
write('Text'); { Output }
writeln('Text'); { Output + newline }

6. Arithmetic Operators

+ - * / div mod

7. Control Structures

If...Then...Else

if age > 18 then


writeln('Adult')
else
writeln('Minor');

Case...Of

case grade of
'A': writeln('Excellent');
'B': writeln('Good');
else writeln('Unknown');
end;

While Loop

while x < 10 do
begin
writeln(x);
x := x + 1;
end;

Repeat...Until Loop

repeat
writeln(x);
x := x + 1;
until x = 10;

For Loop

for i := 1 to 5 do
writeln(i);

8. Procedures and Functions

Procedure

procedure SayHello;
begin
writeln('Hello!');
end;

Function

function Square(x: Integer): Integer;


begin
Square := x * x;
end;
20 Pascal Exercises with Detailed
Explanations and Solutions
Exercise 1: Hello World

Concept: Display a simple message using writeln.

program Hello;
begin
writeln('Hello, world!');
end.

Exercise 2: Sum of Two Numbers

Concept: Read input values, compute and print their sum.

program Sum;
var
a, b, sum: Integer;
begin
readln(a, b);
sum := a + b;
writeln('Sum = ', sum);
end.

Exercise 3: Check Even or Odd

Concept: Use modulus (mod) to check divisibility by 2.

program EvenOdd;
var
n: Integer;
begin
readln(n);
if n mod 2 = 0 then
writeln('Even')
else
writeln('Odd');
end.

Exercise 4: Factorial

Concept: Use loop to multiply numbers from 1 to n.

program Factorial;
var
n, i, f: Integer;
begin
readln(n);
f := 1;
for i := 1 to n do
f := f * i;
writeln('Factorial = ', f);
end.

Exercise 5: Fibonacci Series

Concept: Generate series where each number is the sum of previous two.

program Fibonacci;
var
a, b, c, i, n: Integer;
begin
readln(n);
a := 0;
b := 1;
write(a, ' ', b, ' ');
for i := 3 to n do
begin
c := a + b;
write(c, ' ');
a := b;
b := c;
end;
end.

Exercise 6: Prime Number Check

Concept: Check if a number has only two divisors: 1 and itself.

program PrimeCheck;
var
n, i: Integer;
isPrime: Boolean;
begin
readln(n);
isPrime := true;
for i := 2 to n div 2 do
if n mod i = 0 then
isPrime := false;
if isPrime then
writeln('Prime')
else
writeln('Not Prime');
end.

Exercise 7: Multiplication Table

Concept: Use loop to print multiples of a number.

program Table;
var
i, n: Integer;
begin
readln(n);
for i := 1 to 10 do
writeln(n, ' x ', i, ' = ', n * i);
end.

Exercise 8: Reverse a Number

Concept: Extract digits using mod and reverse with div.

program Reverse;
var
n, rev, r: Integer;
begin
readln(n);
rev := 0;
while n > 0 do
begin
r := n mod 10;
rev := rev * 10 + r;
n := n div 10;
end;
writeln('Reversed = ', rev);
end.

Exercise 9: Palindrome Number

Concept: Compare original number with its reverse.

program Palindrome;
var
n, temp, rev, r: Integer;
begin
readln(n);
temp := n;
rev := 0;
while temp > 0 do
begin
r := temp mod 10;
rev := rev * 10 + r;
temp := temp div 10;
end;
if rev = n then
writeln('Palindrome')
else
writeln('Not Palindrome');
end.

Exercise 10: Greatest of 3 Numbers

Concept: Use conditional comparisons.

program Greatest;
var
a, b, c, max: Integer;
begin
readln(a, b, c);
max := a;
if b > max then
max := b;
if c > max then
max := c;
writeln('Max = ', max);
end.
More Pascal Exercises
Exercise 11: Count Digits in a Number

Concept: Use a loop to count how many times we divide by 10.

program CountDigits;
var
n, count: Integer;
begin
readln(n);
count := 0;
while n <> 0 do
begin
n := n div 10;
count := count + 1;
end;
writeln('Number of digits = ', count);
end.

Exercise 12: Sum of Digits

Concept: Use mod to extract digits and sum them.

program SumDigits;
var
n, sum, r: Integer;
begin
readln(n);
sum := 0;
while n > 0 do
begin
r := n mod 10;
sum := sum + r;
n := n div 10;
end;
writeln('Sum of digits = ', sum);
end.

Exercise 13: Check Leap Year Exercise 14: Simple Calculator


Concept: Apply the leap year rule. Concept: Use case structure for arithmetic
operations.
program LeapYear;
var program Calculator;
year: Integer; var
begin a, b: Real;
readln(year); op: Char;
if ((year mod 4 = 0) and begin
(year mod 100 <> 0)) or readln(a, b);
(year mod 400 = 0) then readln(op);
writeln('Leap Year') case op of
else '+': writeln(a + b);
writeln('Not a Leap Year'); '-': writeln(a - b);
end. '*': writeln(a * b);
'/': if b <> 0 then
writeln(a / b)
else
writeln('Divide by zero!');
else writeln('Invalid operator');
end;
end.

Exercise 15: Power of a Number

Concept: Multiply base by itself exponent times.

program Power;
var
base, exp, i, result: Integer;
begin
readln(base, exp);
result := 1;
for i := 1 to exp do
result := result * base;
writeln('Result = ', result);
end.

Exercise 16: Area of a Circle Exercise 17: Celsius to Fahrenheit


Concept: Use formula: Area = PI * r * r. Concept: Use formula: F = C * 9/5 + 32.

program CircleArea; program Temperature;


const var
PI = 3.14159; C, F: Real;
var begin
r, area: Real; readln(C);
begin F := (C * 9 / 5) + 32;
readln(r); writeln('Fahrenheit = ', F:0:2);
area := PI * r * r; end.
writeln('Area = ', area:0:2);
end.
More Pascal Exercises
Exercise 18: Count Vowels Exercise 19: Swap Numbers

This program counts the number of vowels in a This program swaps the values of two integer
given string. It iterates through each character variables using a temporary variable. The initial
of the string and checks if it is a vowel (a, e, i, value of the first variable (a) is stored in the
o, u). A counter is incremented for each vowel temporary variable, then the value of the
found. second variable (b) is assigned to a, and
finally, the value from the temporary variable is
Code:
assigned to b.

Code:
program CountVowels;
var
str: string; program SwapNumbers;
i, count: Integer; var
begin a, b, temp: Integer;
readln(str); begin
count := 0; readln(a, b);
for i := 1 to length(str) do temp := a;
begin a := b;
case str[i] of b := temp;
'a', 'e', 'i', 'o', 'u', writeln('a = ', a, ', b = ', b);
'A', 'E', 'I', 'O', 'U': count := count + 1; end.
end;
end;
writeln('Number of vowels = ', count);
end.

You might also like