The document contains 10 programs written in Pascal programming language to perform various operations on arrays and matrices. Program 1 multiplies two matrices. Program 2 finds the largest and smallest number in a matrix. Program 3 arranges numbers in an array in ascending order. Program 4 finds the sum of rows and columns of a matrix. The remaining programs perform operations like addition of matrices, displaying diagonal elements, even numbers in an array, upper triangular elements of a matrix, sum of diagonal elements, and sum of odd elements in an array.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100%(5)100% found this document useful (5 votes)
6K views38 pages
Pascal Matrix Operations Examples
The document contains 10 programs written in Pascal programming language to perform various operations on arrays and matrices. Program 1 multiplies two matrices. Program 2 finds the largest and smallest number in a matrix. Program 3 arranges numbers in an array in ascending order. Program 4 finds the sum of rows and columns of a matrix. The remaining programs perform operations like addition of matrices, displaying diagonal elements, even numbers in an array, upper triangular elements of a matrix, sum of diagonal elements, and sum of odd elements in an array.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
{*Program 1*}
(*FY B C A*)
{program: To find multiplication of two matrices}
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:To find larger and smaller no in a matrix}
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:
Ener the elements for the matrix
123 654 789 Max 9Min 1 {*Program 3*} (*FY B C A*)
{program: To arrange no’s in ascending order from the array}
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:
Insert The elements for the array
123 654 789 Ascending Arrangement of array 1 2 3 4 5 6 7 8 9 {*Program 4*} (*FY B C A*)
{program: To find the sum of rows and columns of matrix}
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:
enter the elements for the matrix
565 432 578 Sum of Row and Column 16 9 20 14 16 15 {*Program 5*} (*FY B C A*)
{program : To find addition of two matrices}
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 : To Display even numbers in an array}
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:
Enter array elements
1 2 3 4 5 6 7 8 9 10 Even Elements from Array are 2 4 6 8 10 {*Program 8*} (*FY B C A*)
{program : To display upper triangular elements of matrix 3*3 order}
program matrix; uses crt; var \\{Variable declaration} a:array [1..3,1..3] of integer; i,j:integer; begin clrscr; \\{Beginning of for loop for reading} for i:=1 to 3 do begin for j:=1 to 3 do begin read(a[i,j]); end; end; writeln ('Upper Triangular Matrix is'); for i:=1 to 3 do begin \\{ Beginning of for loop for writing } for j:=1 to 3 do begin if(j>i) then writeln(a[i,j]); end; end; readkey; \\{End of Program} end.
Output: 123 456 789 Upper Triangular Matrix is 2 3 6 {*Program 9*} (*FY B C A*)
{program : To Display the sum of diagonal elements of matrix 3*3 order}
program matrixdisum; uses crt; var a:array [1..3,1..3] of integer; \\{variable declaration} i,j,sum:integer; begin clrscr; \\{for loop for reading matrix} for i:=1 to 3 do begin for j:=1 to 3 do begin read(a[i,j]); end; end; for i:=1 to 3 do \\{for loop for writing matrix} begin for j:=1 to 3 do begin if(i=j) then sum:=sum+a[i,j]; end; end; writeln(‘Sum of Diagonal Elements is ’,sum); readkey; \\ {end of program} end.
Output: 123 123 123
Sum of Diagonal Elements 6
{*Program 10*} (*FY B C A*)
{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:
How many elements do you want to enter in array
10
1 2 3 4 5 6 7 8 9 10Sum of all odd elements in array 25 {*Program 11*} (*FY B C A*)
{program : To check whether the given no is Armstrong no. or not}
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 : To find Fibonacci series up to given no.}
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:
Enter a no of terms required
5 The Fibonacci series is 0 1 1 2 3 {*Program 13*} (*FY B C A*)
{program : To convert a binary no into its decimal equivalent}
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:
ENTER THE NUMBER
36 Binary form of entered no is 10010 {*Program 14*} (*FY B C A*)
{program : To add the series of given no.}
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:
enter range of series
12 enter value for series 5 Sum of Series 0.250 {*Program 15*} (*FY B C A*)
{program : To add the series of given no.}
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:
Enter range of series
23 Sum of Series 2.718 {*Program 16*} (*FY B C A*)
{program : To convert a binary no. into its decimal equivalent }
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*)
{program : To check whether a given no. is prime or not}
program prime; uses crt; var i,no:integer; p:boolean; \\{variable declaration} begin clrscr; writeln('Enter a no'); read(no); i:=2; while(i<=no div 2) do \\{using while do command} begin if(no mod i=0) then \\{using if then command} p:=true; i:=i+1; end; if (p=true) then begin writeln('NOT prime'); \\{displaying no is prime} end else begin writeln('IT IS A PRIME NO'); \\{displaying number is not prime} end; readkey; end. \\{end of program} Output:
Enter a no 23 IT IS A PRIME NO {*Program 18*} (*FY B C A*)
{program : To display the largest among the three given no’s}
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*)
{program : To program series;
uses crt; var i,x,r,j:integer; {variable declaration} sum,f:real; begin clrscr; sum:=0; f:=1; writeln('Enter range of series'); {entering range of series} readln(r); for i:=1 to r do {calculating series} begin for j:=1 to i do begin f:=f*i; end; sum:=sum+f; end; writeln(sum:3:3); {display of sum} readkey; end. {end of program}
Output Enter range of series 2 Sum 5 {*Program 20*} (*FY B C A*)
(*write a program to display all prime numbers between 100 to 500.*)
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.
{program : To display table between two accepted no. in proper format}
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
{program : To copy the contet of one file to another}
program copyfile; uses crt; var s,d : text; \\{variable declaration} x:char; begin \\ {identifying file} assign(s,'[Link]'); assign(d,'[Link]'); 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:
[Link] my name is Vivek kumar. [Link] my name is Vivek kumar. {*Program 23*} (*FY B C A*)
{program : To find no. of “a” in a text file }
program findchar_count; uses crt; var \\{variable identification} s: text; x:char; i:integer; begin clrscr; assign(s,'[Link]'); \\{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:
[Link] My name is Vivek kumar..
No of A’s in File is 2 {*Program 24*} (*FY B C A*)
{program : To display tabs,char,lines in a text file}
{program copyfile; Uses crt; var \\{declaration of variable} s: text; x:char; i,j,k:integer; begin clrscr; assign(s,'[Link]'); 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,'[Link]'); \\{assigning file variables} assign(f,'[Link]'); 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: [Link] My name is indira. [Link] .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,'[Link]'); \\{assigning file variables} assign(o,'[Link]'); assign(e,'[Link]'); 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:
[Link] 123456789 [Link] 13579 [Link] 2468 {*Program 27*} (*FY B C A*)
{program:To concantenate two text files}
program concatenate; uses crt; var f:text; \\{variable declaration} s:file of char; x:char; size:longint; begin clrscr; assign(s,'[Link]'); \\{assigning file variables} assign(f,'[Link]'); 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:
[Link] My name is Rohit. [Link] I am from India. [Link] 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,'[Link]'); \\{assigning file variables} assign(p,'[Link]'); assign(n,'[Link]'); 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:
[Link] 1 -3 4 5 -6 7 9 [Link] -3-6 [Link] 145798 {*Program 29*} (*FY B C A*)
program toupper; uses crt; var s,o:text; {variable declaration} x:char; begin clrscr; assign(s,'[Link]'); {assigning file variables} assign(o,'[Link]'); 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: [Link] my name is rohit [Link] 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:\[Link] Enter new name with path for this file C:\[Link] C:\[Link] is renamed to C:\[Link] {*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
Aditya 135 50 Avadhoot 134 36 Vinayak 140 40 {*Program 32*} (*FY B C A*) {Program : To accept information of 5 employees & display whose name = Satish} 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('Employee whose name is Satish'); for i:=1 to 5 do \\{applying logic} begin if( s[i].name='Satish') then begin writeln(s[i].name,' ',s[i].codeno,' ' , s[i].age); \\{displaying req records} end; readkey; end; end. \\{end of program}
{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*)
(*write a program to swap two numbers using function*)
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:
Enter two numbers
9 56 The numbers after swaping are 56 9 {*Program 36*} (*FY B C A*)
{*write a program to x to the power n using parameter passing in procedure*}
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:
Enter base and power
2 4 2 to power 4 is 16 {*Program 37*} (*FY B C A*)
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*)
{Program : To find sum of digits of given no.’s using recursive function}
program digsum; uses crt; var no:integer; function sum(n:integer):integer; begin \\{beginning of the function} if(n<=0) then begin sum:=0; end else begin sum:=n mod 10 + sum(n div 10); \\{calculating result} end; end; \\{end of the function} begin \\{beginning of main program} clrscr; writeln('Enter a number'); readln(no); writeln('The digit sum is'); writeln(sum(no)); \\{displaying the result} readkey; end. \\{end of program} Output: Enter a number 963 The digit sum is 18