Pascal Programs
Pascal Programs
(*FY B C A*)
program matrix(multi);
uses crt;
var
a,b,c:array [1..3,1..3] of integer;
i,j,k:integer;
begin \\{beginning of the program}
clrscr;
writeln('first matrix');
for i:=1 to 3 do \\{beginning of first for loop}
begin
for j:=1 to 3 do
begin
read(a[i,j]);
end;
end;
writeln('second matrix');
for i:=1 to 3 do
begin \\{end of first for loop}
for j:=1 to 3 do \\{beginning of second for loop}
begin
read(b[i,j]);
end;
end; \\{end of second for loop}
for i:=1 to 3 do \\{beginning of third for loop}
begin
for j:=1 to 3 do
begin
c[i,j]:=0;
for k:=1 to 3 do
begin
c[i,j]:=c[i,j]+a[i,k]*b[k,j];
end;
end;
end; \\{end of third for loop}
writeln('Sum of Matrices')
for i:=1 to 3 do
begin
for j:=1 to 3 do
begin
write(c[i,j]:3);
end;
writeln;
end;
end.
\\{end of the program}
Output:
first matrix
111
111
111
second matrix
222
222
222
Multiplication of Matrices
666
666
666
{*Program 2*}
(*FY B C A*)
program matrix(max,min);
uses crt;
var
a:array [1..3,1..3] of integer;
i,j,min,max:integer;
begin \\{beginning of the program}
clrscr;
max:=0; \\{assigning value of max to 0}
writeln('enter the elements for the matrix')
for i:=1 to 3 do \\{beginning of for loop}
begin
for j:=1 to 3 do
begin
read(a[i,j]);
end;
end; \\{end of for loop}
min:=a[i,j];
for i:=1 to 3 do \\{beginning of for loop}
begin
for j:=1 to 3 do \\{end of for loop}
begin
if(max<a[i,j]) then
max:=a[i,j];
if(min>a[i,j]) then
min:=a[i,j];
end;
end;
writeln('max ',max); \\{displaying the max value}
writeln('min ',min); \\{displaying the min value}
readkey;
end. \\{end of the program}
Output:
program arrassending;
uses crt;
var
a:array [1..10] of integer;
i,j,temp:integer;
begin \\{beginning of the program}
clrscr;
writeln('insert the elements for the array');
for i:=1 to 10 do \\{taking the elements in an array}
read(a[i]);
for i:=1 to 9 do \\{beginning of for loop}
begin
for j:=i+1 to 10 do
begin
if(a[i]>a[j]) then
begin
temp:=a[i];
a[i]:=a[j];
a[j]:=temp;
end;
end;
end; \\{end of for loop}
writeln(‘Ascending Arrangement of array’);
for i:=1 to 10 do
write(a[i]:3);
readkey;
end. \\{end of the program}
Output:
program matrix;
uses crt;
var
a:array [1..3,1..3] of integer;
i,j,r,c:integer;
begin \\{beginning of the program}
clrscr;
writeln('enter the elements for the matrix');
for i:=1 to 3 do \\{reading matrix}
begin
for j:=1 to 3 do
begin
read(a[i,j]);
end;
end;
for i:=1 to 3 do
begin
for j:=1 to 3 do
begin
r:=r+a[i,j] \\{applying logic}
end;
writeln(r);
r:=0;
end;
writeln(‘Sum of Row and Column’);
for j:=1 to 3 do
begin
for i:=1 to 3 do
begin
c:=c+a[i,j]
end;
writeln(c);
c:=0;
end;
readkey;
end. \\{end of the program}
Output:
program matrix(add);
uses crt;
var
a,b,c:array [1..3,1..3] of integer; \\{variable declarations}
i,j:integer;
begin
clrscr;
writeln (‘Enter 1st matrix’);
for i:=1 to 3 do \\{beginning of for loop matrix 1}
begin
for j:=1 to 3 do
begin
read(a[i,j]);
end;
end;
writeln (‘Enter 2nd matrix’);
for i:=1 to 3 do
begin \\{beginning of for loop matrix 2}
for j:=1 to 3 do
begin
read(b[i,j]);
end;
end;
for i:=1 to 3 do \\{adding 2 matrix}
begin
for j:=1 to 3 do
begin
c[i,j]:=a[i,j]+b[i,j];
end;
end;
writeln(‘Addition of Matrices ‘);
for i:=1 to 3 do
begin
for j:=1 to 3 do
begin \\{display}
write(c[i,j]:3);
end;
writeln;
end;
readkey;
end. \\{end of program}
Output:
Enter 1st matrix
111
222
333
Enter 2nd matrix
111
222
333
Addition of Matrices
222
444
666
{*Program 6*}
(*FY B C A*)
program matrixdiago;
uses crt;
var
a:array [1..5,1..5] of integer; \\{variable declaration}
i,j:integer;
begin
clrscr; \\{beginning of for loop for reading}
for i:=1 to 5 do
begin
for j:=1 to 5 do
begin
read(a[i,j]);
end;
end;
writeln(‘Diagonal Elements are’);
for i:=1 to 5 do \\{beginning of for loop for displaying}
begin
for j:=1 to 5 do
begin
if(i=j) then
writeln(a[i,j]);
end;
end;
readkey; \\ {end of program}
end.
Output:
11111
22222
33333
44444
55555
Diagonal Elements are
1
2
3
4
5
{*Program 7*}
(*FY B C A*)
program arrayeven;
uses crt;
var
a:array [1..10] of integer; \\{variable description}
i,j:integer;
begin
clrscr;
writeln(‘Enter array elements’);
for i:=1 to 10 do \\{beginning of for loop for reading matrix}
read(a[i]);
writeln(‘Even Elements from Array are’);
for i:=1 to 10 do
begin
if(a[i] mod 2 = 0) then \\{writing of condition}
writeln(a[i]);
end;
readkey;
end. \\{end of program}
Output:
Output:
123
456
789
Upper Triangular Matrix is
2
3
6
{*Program 9*}
(*FY B C A*)
Output:
123
123
123
{program : To
program arrayoddsum;
uses crt;
var {variable declaration}
a:array [1..100] of integer;
i,j,n,sum:integer;
begin
clrscr;
sum:=0;
writeln('How many elements do you want to enter in array');
readln(n); {for loop for reading elements in array}
for i:=1 to n do
read(a[i]); {for loop for writing elements in array}
read(a[i]);
for i:=1 to n do
begin
if(a[i] mod 2 <> 0) then
sum:=sum+a[i];
end;
writeln('Sum of all odd elements in array',sum);
readkey; {end of program}
end.
Output:
1
2
3
4
5
6
7
8
9
10Sum of all odd elements in array 25
{*Program 11*}
(*FY B C A*)
program armstrong;
uses crt;
var
a,b,c,temp:integer; \\{variable declaration}
begin
clrscr;
c:=0;
writeln(Enter a no:’); \\{entering a no for checking}
read(a);
temp:=a;
while(a>0) do
begin
b:=(a mod 10);
a:=(a div 10);
c:= c+(b*b*b);
end;
if(c=temp) then \\{if condition matches then is Armstrong no}
begin
writeln('Its Armstrong Number');
end
else \\{if condition doesn’t matches then it isn't
Armstrong no}
begin
writeln('Not Armstrong Number');
end;
end;
readkey;
end. \\ {end of program}
Output:
Enter a no :
153
Its Armstrong number
{*Program 12*}
(*FY B C A*)
program fibonacci;
uses crt;
var
a,f0,f1,f2,temp:integer; \\{variable declaration}
begin
clrscr;
f0:=0;
f1:=1;
temp:=3;
writeln('enter a no of terms required');
read(a);
writeln('The Fibonacci series is ');
writeln(f0);
writeln(f1);
while(temp<=a) do \\{using while do command}
begin
f2:=f0+f1;
writeln(f2);
f0:=f1;
f1:=f2;
temp:=temp+1;
end;
readkey;
end. \\{end of program}
Output:
program binartod;
uses crt;
var
no,rem,i:integer; \\{variable declaration}
a:array[1..10] of integer;
begin
clrscr;
writeln('ENTER THE NUMBER');
read(no);
i:=0;
while no<>0 do \\{logic block}
begin
rem:=no mod 2;
a[i]:=rem;
i:=i+1;
no:=no div 2;
end;
i:=i-1;
writeln(‘Binary form of entered no is’);
while i>0 do
begin
write(a[i]);
i:=i-1;
end;
readkey;
end. \\{end of program}
Output:
program series;
uses crt;
var
i,x,r:integer; \\{variable declaration}
sum,y:real;
begin
clrscr;
sum:=0;
y:=1;
writeln('enter range of series');
readln(r);
writeln('enter value for series');
readln(x);
for i:=1 to r do \\{beginning of for loop}
begin
y:=y*x;
sum:=sum+1/y;
end;
writeln(‘Sum of Series’);
writeln(sum:3:3);
readkey;
end. \\{end of program}
Output:
program series;
uses crt;
var
i,x,r:integer; \\{variable declaration}
sum,f:real;
begin
clrscr;
sum:=0;
f:=1;
writeln('Enter range of series');
readln(r);
for i:=1 to r do \\{beginning of for loop}
begin
f:=f*i;
sum:=sum+i/f;
end;
writeln(‘Sum of Series’);
writeln(sum:3:3);
readkey;
end. \\{end of program}
Output:
program bitode;
var
p,m,d,no:integer; \\ {variable declaration}
begin
p:=1;d:=0;
writeln('Enter a binary no'); \\ {reading from user}
readln(no);
while(no>0) do \\ {logic block}
begin
m:=no mod 10;
d:=d+m*p;
no:=no div 10;
p:=p*2;
end;
writeln(‘Decimal form of entered binary ’,d); \\ {display result}
end.
Output:
Enter a binary no
10010
Decimal form of entered binary 18
{*Program 17*}
(*FY B C A*)
Enter a no
23
IT IS A PRIME NO
{*Program 18*}
(*FY B C A*)
program larger;
uses crt;
var
p,q,r,max:integer; \\{variable declaration}
begin
clrscr;
writeln('enter three nos ');
read(p,q,r);
if (p>q)then \\{using if else command}
max:=p
else
max:=q;
if (max>r) then
max:=max
else
max:=r;
writeln('The largest of these three nos is ',max); \\{displaying largest in matrix}
readkey;
end. \\{end of program}
Output:
enter three nos
6
3
9
The largest of these three no is 9
{*Program 19*}
(*FY B C A*)
Output
Enter range of series
2
Sum
5
{*Program 20*}
(*FY B C A*)
program san;
uses crt;
var
i,j,k:integer; \\{declaration of variables}
a:integer;
begin
clrscr;
for i:=100 to 500 do \\{logic block}
begin
a:=1;
for j:=2 to i-1 do
begin
if (i mod j)=0 then
begin
j:=i-1;
a:=0;
end;
end;
if a=1 then
write(i,' '); \\{display}
end;
end.
OUTPUT SCREEN:
101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 197 199 211 223 227 224
233 239 241
251 257 263 369 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389
397 401 409
419 421 431 433 439 443 449 457 461 463 467 479 487 491 499
{*Program 21*}
(*FY B C A*)
program table;
var i,j,no1,no2,res:integer; \\{variable declaration}
begin
writeln('enter no1'); \\{input from user of range}
readln(no1);
writeln('enter no2');
readln(no2);
writeln(‘Tables’);
for i:=no1 to no2 do
begin
for j:=1 to 10 do \\{calculation of each table}
begin
res:=i*j;
writeln(i,'*',j,'=',res); \\{displaying each term}
end;
readln;
end;
end.
Output:
Enter Starting No
1
Enter Ending No
2
Tables
1*1 = 1
1*2 = 2
1*3 = 3
1*4 = 4
1*5 = 5
1*6 = 6
1*7 = 7
1*8 = 8
1*9 = 9
1*10 = 10
2*1 = 2
2*2 = 4
2*3 = 6
2*4 = 8
2*5 = 10
2*6 = 12
2*7 = 14
2*8 = 16
2*9 = 18
2*10 = 20
{*Program 22*}
(*FY B C A*)
program copyfile;
uses
crt;
var
s,d : text; \\{variable declaration}
x:char;
begin \\ {identifying file}
assign(s,'source.txt');
assign(d,'dest.txt');
reset(s); \\ {opening the s file}
rewrite(d); \\ {read till end of file}
while NOT eof(s) do
begin \\{read till end of each line}
while NOT eoln(s) do
begin
read(s,x); \\ {read from s & store in x}
write(d,x); \\ {write in d from x}
end;
readln(s);
writeln(d); \\{display Output:}
end;
close(s); \\ {close file s & d}
close(d);
end.
Output:
source.txt
my name is Vivek kumar.
dest.txt
my name is Vivek kumar.
{*Program 23*}
(*FY B C A*)
program findchar_count;
uses crt;
var \\{variable identification}
s: text;
x:char;
i:integer;
begin
clrscr;
assign(s,'source.txt'); \\{identifying file}
reset(s); \\{opening file}
i:=0;
while NOT eof(s) do
begin
while NOT eoln(s) do
begin
read(s,x);
if(x='a') then
begin
i:=i+1;
end;
end;
readln(s);
end;
writeln('No of As in File is',i);
close(s); \\{closing file}
readkey;
end. \\{end of program}
\
Output:
source.txt
My name is Vivek kumar..
No of A’s in File is 2
{*Program 24*}
(*FY B C A*)
{program copyfile;
Uses crt;
var \\{declaration of variable}
s: text;
x:char;
i,j,k:integer;
begin
clrscr;
assign(s,'source.txt');
reset(s);
i:=0;
j:=0;
k:=0;
while NOT eof(s) do \\{reading file and using conditions}
begin
while NOT eoln(s) do
begin
read(s,x);
if(((ord(x)>123) and (ord(x)<96)) or ((ord(x)<91) and (ord(x)>64)))then
begin
i:=i+1;
end;
if(ord(x)=10) then
begin
j:=j+1;
end;
if(ord(x)=9) then
begin
k:=k+1;
end;
end;
readln(s);
end;
writeln('No of lines in File is',k); \\{display Output:}
writeln('No of Tabs in file is ',j);
writeln('No of characters in file is',i);
close(s);
readkey;
end. \\{end of program}
Output:
No of lines in File is 3
No of Tabs in file is 3
No of characters in file is 100
{*Program 25*}
(*FY B C A*)
program : To display the content of a file in reverse order}
program reverse;
uses crt;
var \\{variable declaration}
f:text;
s:file of char;
x:char;
size:longint;
begin
clrscr;
assign(s,'source.txt'); \\{assigning file variables}
assign(f,'file1.txt');
reset(s);
reset(f);
size:=filesize(s);
seek (s,size);
while NOT eof(s) do \\{read and applying logic over files}
begin
while NOT eoln(s) do
begin
while size>0 do
begin
read(s,x);
write(s,x);
size:=size-1;
end;
end;
readln(f);
end;
close(f);
close(s);
end.
Output:
Source.txt
My name is indira.
File1.txt
.aridni si eman yM
{*Program 26*}
(*FY B C A*)
{program : To accept 10 natural no’s in a file & create two files as “oddtext” & “eventext” to store odd
7even no’s respectively}
program oddevenfile;
uses crt;
var
s,o,e:text; \\{variable declaration}
x:char;
begin
clrscr;
assign(s,'source.txt'); \\{assigning file variables}
assign(o,'odd.txt');
assign(e,'even.txt');
reset(s);
rewrite(e);
rewrite(o);
while NOT eof(s) do \\{read and apply logic for prog}
begin
while NOT eoln(s) do
begin
read(s,x);
if(odd(ord(x))) then
begin
write(o,x);
end
else
begin
write(e,x);
end;
end;
readln(s);
writeln(e);
writeln(o);
end;
close(e);
close(o);
close(s);
end. \\{end of program}
Output:
Source.txt
123456789
Odd.txt
13579
Even.txt
2468
{*Program 27*}
(*FY B C A*)
program concatenate;
uses crt;
var
f:text; \\{variable declaration}
s:file of char;
x:char;
size:longint;
begin
clrscr;
assign(s,'source.txt'); \\{assigning file variables}
assign(f,'file1.txt');
reset(s);
reset(f);
size:=filesize(s);
seek (s,size);
while NOT eof(f) do \\{read and apply logic for prog}
begin
while NOT eoln(f) do
begin
read(f,x);
write(s,x)
end;
readln(f);
end;
close(f);
close(s);
end. \\{end of program}
Output:
Source.txt
My name is Rohit.
File1.txt
I am from India.
Source.txt after execution of program
My name is RohitI am from India.
{*Program 28*}
(*FY B C A*)
{Program : To accept 10 integers in a file and create two files as “positive text “ & “negative text” to store
positive & negative numbers respectively}
program positive_negative_file;
uses crt;
var
s,o,e:text; \\{variable declaration}
x:char;
begin
clrscr;
assign(s,'source.txt'); \\{assigning file variables}
assign(p,'positive.txt');
assign(n,'negative.txt');
reset(s);
rewrite(p);
rewrite(n);
while NOT eof(s) do \\{read and apply logic for prog}
begin
while NOT eoln(s) do
begin
read(s,x);
if((ord(x)>=-1)) then
begin
write(p,x:3);
end
else
begin
write(n,x:3);
end;
end;
readln(s);
writeln(n);
writeln(p);
end;
close(s);
close(n);
close(p);
end. \\{end of program}
Output:
Source.txt
1 -3 4 5 -6 7 9
Negative.txt
-3-6
Positive.txt
145798
{*Program 29*}
(*FY B C A*)
program toupper;
uses
crt;
var
s,o:text; {variable declaration}
x:char;
begin
clrscr;
assign(s,'source.txt'); {assigning file variables}
assign(o,'upper.txt');
reset(s);
rewrite(o);
while NOT eof(s) do { read and apply logic for prog }
begin
while NOT eoln(s) do
begin
read(s,x);
write(o,UpCase(x));
end;
readln(s);
writeln(o);
end;
close(o);
close(s);
end. {end of program}
Output:
Source.txt
my name is rohit
Upper.txt
MY NAME IS ROHIT
{*Program 30*}
(*FY B C A*)
program rename;
uses
crt;
var
s,d : text; \{variable declearation}
x:char;
name,new:string;
begin
clrscr;
writeln('Enter File name with path to rename');
readln(name);
writeln('Enter new name with path for this file');
readln(new); {new name of file}
assign(s,name);
rename(s,new);
writeln(name,' is renamed to ',new);
readln;
end. {end of program}
Output:
Enter File name with path to rename
C:\rohit.txt
Enter new name with path for this file
C:\bca.txt
C:\rohit.txt is renamed to C:\bca.txt
{*Program 31*}
(*FY B C A*)
{Program : To accept information of five employees & display the employee whose age is above 35 }
program records;
uses crt;
type
employee=record \\{defining record}
codeno:integer;
name:string[25];
age:integer;
end;
var
s:array[1..5] of employee;
i:integer;
begin
clrscr;
writeln('Enter Name, Code, Age of 5 employees');
for i:=1 to 5 do
begin
read(s[i].name,s[i].codeno,s[i].age); \\{reading records}
end;
writeln(‘Employees whose ages are more than 35’);
for i:=1 to 5 do \\{applying logic}
begin
if( s[i].age>35) then
begin
writeln(s[i].name,' ',s[i].codeno,' ' , s[i].age); \\{displaying req records}
end;
end;
readkey;
end. \\{end of program
Output:
Output:
{Program : To accept information of 5 students & display the details of top three students}
program records;
uses crt;
type
student=record \\{defining records}
roll:integer;
name:string[25];
m1,m2,tot:longint;
percent:real;
end;
var
s:array[1..5] of student;
i,j,max:integer;
temp:student;
begin
writeln('Enter Name, Roll no, Marks of 2 sub of 5 student');
for i:=1 to 5o do
begin
read(s[i].name,s[i].roll,s[i].m1,s[i].m2); \\{reading records}
end;
for i:=1 to 5 do
begin
s[i].tot:=s[i].m1+s[i].m2;
s[i].percent:=s[i].tot/2;
end;
for i:=1 to 5 do \\{applying logic}
begin
for j:=i+1 to 5 do
begin
if(s[i].tot>s[j].tot) then
begin
temp:=s[i];
s[i]:=s[j];
s[j]:=temp;
end;
end;
end;
writeln('Top 3 students are');
for i:=3 downto 1 do \\{displaying records}
begin
writeln(s[i].name,' ', s[i].roll,' ' , s[i].tot:2,' ',s[i].percent:2); {toppers are}
end;
end. \\{end of program}
Output:
Rohit 1 60 65
Ram 2 65 55
Sanket 3 56 67
Sayali 4 67 66
Shamal 5 56 67
Top 3 students are
Sayali 4 133 6.7E+01
Rohit 1 125 6.3E+01
Shamal 5 123 6.2E+01
{*Program 34*}
(*FY B C A*)
program records;
uses
crt;
type
student=record {defining records}
roll:integer;
name:string[25];
m1,m2,tot:longint;
percent:real;
end;
var
s:array[1..50] of student;
i,j,max:integer;
temp:student;
begin
clrscr;
writeln('Enter Name,Roll no,Marks of 2 sub of 50 student');
for i:=1 to 50 do
begin
readln(s[i].name,s[i].roll,s[i].m1,s[i].m2); {reading records}
end;
for i:=1 to 50 do
begin
s[i].tot:=s [i].m1+s[i].m2;
end;
writeln('Students Records Are as Follows');
for i:=1 to 50 do {displaying records}
begin
writeln(s[i].name,' ', s[i].roll,' ' , s [i].tot:2,' ',s [i].percent:2);
end;
readkey;
end.
{*Program 35*}
(*FY B C A*)
program swap2;
uses crt;
var
n1,n2:integer;
function swap(a,b:integer):integer;
var
temp:integer;
begin \\{beginning of the function}
writeln('Enter two numbers');
readln(a,b);
temp:=a;
a:=b;
b:=temp;
writeln('The numbers after swaping are');
writeln(a);
writeln(b);
end; \\{end of the function}
begin \\{beginning of program}
clrscr;
swap(n1,n2); \\{displaying the result}
readkey;
end. \\{end of program}
Output:
program powerx;
uses crt;
var
x,y,i,pow:integer;
function power(x,y:integer):integer;
begin \\{beginning of the function}
pow:=1;
for i:=1 to y do
begin
pow:=pow*x; \\{calculating result}
end;
power:=pow;
end; \\{end of the function}
begin \\{beginning of main program}
clrscr;
writeln('Enter base and power');
readln(x,y);
writeln(x,' to power ' ,y ,' is ');
writeln(power(x,y)); \\{displaying the result}
readkey;
end. \\{end of program}
Output:
program factfuncion;
uses
crt;
var
i,j:integer; {declaring global variables}
function f(k :integer):integer; {declaring function}
begin
if(k=1)then
f:=1
else
begin
f:=k*f(k-1);
end;
end;
begin
clrscr;
writeln('Enter the number:');
read(i);
writeln('The factorial of a number is:',f(i)); {calling function}
end.
OUTPUT: SCREEN:
Enter the number:5
The factorial of a number is:120
{*Program 38*}
(*FY B C A*)