Pascal Programs

  • Uploaded by: vallabha
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Pascal Programs as PDF for free.

More details

  • Words: 3,751
  • Pages: 38
{*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 clrscr; writeln('first matrix'); for i:=1 to 3 do begin for j:=1 to 3 do begin read(a[i,j]); end; end; writeln('second matrix'); for i:=1 to 3 do begin for j:=1 to 3 do begin read(b[i,j]); end; end; for i:=1 to 3 do 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; 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

\\{beginning of the program} \\{beginning of first for loop}

\\{end of first for loop} \\{beginning of second for loop}

\\{end of second for loop} \\{beginning of third for loop}

\\{end of third for loop}

{*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 clrscr; max:=0; writeln('enter the elements for the matrix') for i:=1 to 3 do begin for j:=1 to 3 do begin read(a[i,j]); end; end; min:=a[i,j]; for i:=1 to 3 do begin for j:=1 to 3 do begin if(maxa[i,j]) then min:=a[i,j]; end; end; writeln('max ',max); writeln('min ',min); readkey; end. Output: Ener the elements for the matrix 123 654 789 Max 9Min 1

\\{beginning of the program} \\{assigning value of max to 0} \\{beginning of for loop}

\\{end of for loop} \\{beginning of for loop} \\{end of for loop}

\\{displaying the max value} \\{displaying the min value} \\{end of the program}

{*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 clrscr; writeln('insert the elements for the array'); for i:=1 to 10 do read(a[i]); for i:=1 to 9 do 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; writeln(‘Ascending Arrangement of array’); for i:=1 to 10 do write(a[i]:3); readkey; end.

Output: Insert The elements for the array 123 654 789 Ascending Arrangement of array 1 2 3 4 5 6 7 8 9

\\{beginning of the program} \\{taking the elements in an array} \\{beginning of for loop}

\\{end of for loop}

\\{end of the program}

{*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; i,j:integer; begin clrscr; writeln (‘Enter 1st matrix’); for i:=1 to 3 do 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 for j:=1 to 3 do begin read(b[i,j]); end; end; for i:=1 to 3 do 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 write(c[i,j]:3); end; writeln; end; readkey; end.

\\{variable declarations}

\\{beginning of for loop matrix 1}

\\{beginning of for loop matrix 2}

\\{adding 2 matrix}

\\{display}

\\{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; i,j:integer; begin clrscr; 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 begin for j:=1 to 5 do begin if(i=j) then writeln(a[i,j]); end; end; readkey;

\\{variable declaration} \\{beginning of for loop for reading}

\\{beginning of for loop for displaying}

\\ {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; i,j:integer; begin clrscr; writeln(‘Enter array elements’); for i:=1 to 10 do read(a[i]); writeln(‘Even Elements from Array are’); for i:=1 to 10 do begin if(a[i] mod 2 = 0) then writeln(a[i]); end; readkey; end.

\\{variable description}

\\{beginning of for loop for reading matrix}

\\{writing of condition}

\\{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; 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 begin f2:=f0+f1; writeln(f2); f0:=f1; f1:=f2; temp:=temp+1; end; readkey; end.

\\{variable declaration}

\\{using while do command}

\\{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; a:array[1..10] of integer; begin clrscr; writeln('ENTER THE NUMBER'); read(no); i:=0; while no<>0 do 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. Output: ENTER THE NUMBER 36 Binary form of entered no is 10010

\\{variable declaration}

\\{logic block}

\\{end of program}

{*Program 14*} (*FY B C A*) {program : To add the series of given no.} program series; uses crt; var i,x,r:integer; 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 begin y:=y*x; sum:=sum+1/y; end; writeln(‘Sum of Series’); writeln(sum:3:3); readkey; end.

\\{variable declaration}

\\{beginning of for loop}

\\{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; sum,f:real; begin clrscr; sum:=0; f:=1; writeln('Enter range of series'); readln(r); for i:=1 to r do begin f:=f*i; sum:=sum+i/f; end; writeln(‘Sum of Series’); writeln(sum:3:3); readkey; end.

\\{variable declaration}

\\{beginning of for loop}

\\{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; begin p:=1;d:=0; writeln('Enter a binary no'); readln(no); while(no>0) do begin m:=no mod 10; d:=d+m*p; no:=no div 10; p:=p*2; end; writeln(‘Decimal form of entered binary ’,d); end.

\\ {variable declaration} \\ {reading from user} \\ {logic block}

\\ {display result} 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; begin clrscr; writeln('enter three nos '); read(p,q,r); if (p>q)then max:=p else max:=q; if (max>r) then max:=max else max:=r; writeln('The largest of these three nos is ',max); readkey; end.

\\{variable declaration}

\\{using if else command}

\\{displaying largest in matrix} \\{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; sum,f:real; begin clrscr; sum:=0; f:=1; writeln('Enter range of series'); readln(r); for i:=1 to r do begin for j:=1 to i do begin f:=f*i; end; sum:=sum+f; end; writeln(sum:3:3); readkey; end. Output Enter range of series 2 Sum 5

{variable declaration}

{entering range of series} {calculating series}

{display of sum} {end of program}

{*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; a:integer; begin clrscr; for i:=100 to 500 do 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,' '); end; end.

\\{declaration of variables}

\\{logic block}

\\{display}

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 : To display table between two accepted no. in proper format} program table; var i,j,no1,no2,res:integer; begin writeln('enter no1'); readln(no1); writeln('enter no2'); readln(no2); writeln(‘Tables’); for i:=no1 to no2 do begin for j:=1 to 10 do begin res:=i*j; writeln(i,'*',j,'=',res); end; readln; end; end.

\\{variable declaration} \\{input from user of range}

\\{calculation of each table} \\{displaying each term}

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 : 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,'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 : To find no. of “a” in a text file } program findchar_count; uses crt; var s: text; x:char; i:integer; begin clrscr; assign(s,'source.txt'); reset(s); 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); readkey; end. \

\\{variable identification}

\\{identifying file} \\{opening file}

\\{closing file} \\{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 : 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,'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 f:text; s:file of char; x:char; size:longint; begin clrscr; assign(s,'source.txt'); assign(f,'file1.txt'); reset(s); reset(f); size:=filesize(s); seek (s,size); while NOT eof(s) do 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.

\\{variable declaration}

\\{assigning file variables}

\\{read and applying logic over files}

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; x:char; begin clrscr; assign(s,'source.txt'); assign(o,'odd.txt'); assign(e,'even.txt'); reset(s); rewrite(e); rewrite(o); while NOT eof(s) do 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.

\\{variable declaration}

\\{assigning file variables}

\\{read and apply logic for prog}

\\{end of program}

Output: Source.txt 123456789 Odd.txt 13579 Even.txt 2468

{*Program 27*} (*FY B C A*) {program:To concantenate two text files} program concatenate; uses crt; var f:text; s:file of char; x:char; size:longint; begin clrscr; assign(s,'source.txt'); assign(f,'file1.txt'); reset(s); reset(f); size:=filesize(s); seek (s,size); while NOT eof(f) do begin while NOT eoln(f) do begin read(f,x); write(s,x) end; readln(f); end; close(f); close(s); end.

\\{variable declaration}

\\{assigning file variables}

\\{read and apply logic for prog}

\\{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; x:char; begin clrscr; assign(s,'source.txt'); assign(p,'positive.txt'); assign(n,'negative.txt'); reset(s); rewrite(p); rewrite(n); while NOT eof(s) do 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.

Output: Source.txt 1 -3 4 5 -6 7 9 Negative.txt -3-6 Positive.txt 145798

\\{variable declaration}

\\{assigning file variables}

\\{read and apply logic for prog}

\\{end of program}

{*Program 29*} (*FY B C A*) program toupper; uses crt; var s,o:text; x:char; begin clrscr; assign(s,'source.txt'); assign(o,'upper.txt'); reset(s); rewrite(o); while NOT eof(s) do 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.

Output: Source.txt my name is rohit Upper.txt MY NAME IS ROHIT

{variable declaration}

{assigning file variables}

{ read and apply logic for prog }

{end of program}

{*Program 30*} (*FY B C A*) program rename; uses crt; var s,d : text; 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); assign(s,name); rename(s,new); writeln(name,' is renamed to ',new); readln; end. 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

\{variable declearation}

{new name of file}

{end of program}

{*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 codeno:integer; name:string[25]; age:integer;

\\{defining record}

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); end; writeln(‘Employees whose ages are more than 35’); for i:=1 to 5 do begin if( s[i].age>35) then begin writeln(s[i].name,' ',s[i].codeno,' ' , s[i].age); end; end; readkey; end. Output: Enter Name, Code, Age of 5 employees Rohit 123 33 Aditya 135 50 Avadhoot 134 36 Vinayak 140 40 Rajesh 130 40 Employees whose ages are more than 35 Aditya 135 50 Avadhoot 134 36 Vinayak 140 40

\\{reading records} \\{applying logic}

\\{displaying req records}

\\{end of program

{*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} Output: Enter Name, Code, Age of 5 employees Satish 65 40 Sammer 66 42 Sunil 67 46 Swapnil 68 48 Rajesh 130 40 Employee whose name is Satish Satish 65 40

{*Program 33*} (*FY B C A*) {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 writeln('Enter two numbers'); readln(a,b); temp:=a; a:=b; b:=temp; writeln('The numbers after swaping are'); writeln(a); writeln(b); end; begin clrscr; swap(n1,n2); readkey; end.

\\{beginning of the function}

\\{end of the function} \\{beginning of program} \\{displaying the result} \\{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 pow:=1; for i:=1 to y do begin pow:=pow*x; end; power:=pow; end; begin clrscr; writeln('Enter base and power'); readln(x,y); writeln(x,' to power ' ,y ,' is '); writeln(power(x,y)); readkey; end.

\\{beginning of the function}

\\{calculating result} \\{end of the function} \\{beginning of main program}

\\{displaying the result} \\{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; function f(k :integer):integer; 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)); end.

OUTPUT: SCREEN: Enter the number:5 The factorial of a number is:120

{declaring global variables} {declaring function}

{calling function}

{*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

Related Documents

Pascal Programs
May 2020 4
Pascal
November 2019 47
Pascal
October 2019 35
Pascal
November 2019 37
Pascal
December 2019 54
Pascal
June 2020 9

More Documents from ""