Sunteți pe pagina 1din 15

Prepared by:

Mhar Daniel




TPL ACT.
(MIDTERM PROJECT)








SUBMITTED BY: SUBMITTED TO:
MHAR DANIEL MR. IAN CLINT E. OFFEMARIA
Prepared by:
Mhar Daniel
1. Write a Pascal program that will accept Five Grades of a student and determine the average and display whether the student Pass
or Fail.
Code: (if else) Output:
uses crt; Input 5 grades:
var a,b,c,d,e:real; 74
var ave:real; 75
begin 76
clrscr; 77
writeln('Input 5 grades: '); 78
readln(a,b,c,d,e);
ave:=(a+b+c+d+e)/5; Average is PASSED
writeln; Average is 76.00_
if(ave>74) then begin
writeln('Average is PASSED'); end else
writeln('Average is FAILED!');
write('Average is ',ave:2:2); readln; end.

2. Write a Pascal program that will accept five integers and will determine the smallest and the lowest among the five input
numbers.

Code: (if else) Output:
uses crt; Input 5 integers:
var a,b,c,d,e,big,small:integer; begin clrscr; 5 4 3 2 1
writeln('Input 5 integers:');
readln(a,b,c,d,e); Biggest number: 5
small:=a; big:=a; Smallest number: 1_
if(small>b) then small:=b;
if(big<b) then big:=b;
if(small>c) then small:=c;
if(big<c) then big:=c;
if(small>d) then small:=d;
if(big<d) then big:=d;
if(small>e) then small:=e;
if(big<e) then big:=e; writeln;
writeln('Biggest number: ',big);
write('Smallest number: ',small); readln; end.

3. Write a program that will accept an integer and determine whether an input number is EVEN or ODD (using CASE statement).

Code: Output:
Uses Crt; Input a number: 101
Var r, Number : integer; The number entered is ODD_
Begin Clrscr;
Write(Input a number : );
Readln(number);
r := number mod 2; case r of
0 : Write(The number entered is EVEN);
1 : Write(The number entered is ODD);
end; Readln; End.

4. Write a program that will accept Five Grades of a student, determine the average and display whether the student Pass or Fail
(using CASE statement).

Code: Output:
uses crt; Input 5 grades:
var a,b,c,d,e:integer; 74 75 76 77 78
Prepared by:
Mhar Daniel
var ave:integer;
begin Average is PASSED
clrscr; Average is 76_
writeln('Input 5 grades: ');
readln(a,b,c,d,e);
ave:=(a+b+c+d+e)div 5; writeln; case ave of
75..100 : writeln('Average is PASSED');
0..74 : writeln('Average is FAILED!'); end;
write('Average is ',ave); readln; end.

5. Write a program that determines if the input letter is Vowel or Consonant. Your program must be able to handle a capital or small
input letter.

1
st
Solution: Use the ladderized if/else if /else conditional statements
2
nd
Use the switch/case conditional statement

Code: (if else) Output:
uses crt; Input letter: a
var letter:char; It is a Vowel
begin clrscr; _
write('Input letter: ');
readln(letter);
if(letter='a')OR(letter='A')OR(letter='E')OR(letter='e')OR(letter='I')OR(letter='i')
OR(letter='o')OR(letter='O')OR(letter='U')OR(letter='u')then
writeln('It is a Vowel')
else writeln('It is a Consonant'); readln; end.

Code: (case) Output:
uses crt; Input letter: Z
var letter:char; It is a Consonant
begin clrscr; _
write('Input letter: ');
readln(letter); case letter of
'A':writeln('It is a Vowel');
'a':writeln('It is a Vowel');
'E':writeln('It is a Vowel');
'e':writeln('It is a Vowel');
'I':writeln('It is a Vowel');
'i':writeln('It is a Vowel');
'O':writeln('It is a Vowel');
'o':writeln('It is a Vowel');
'U':writeln('It is a Vowel');
'u':writeln('It is a Vowel');
'a'..'z':writeln('It is a Consonant');
'A'..'Z':writeln('It is a Consonant');
end; readln; end.

6. Write a program that accepts dates written in numerical form and then output them as a complete form. For example, the input:
2 26 1986 should produce the output: February 26, 1986

1
st
Solution: Use the ladderized if/else if /else conditional statements
2
nd
Use the switch/case conditional statement

Code: (if else) Output:
uses crt; Input month: 12
var month,day,year:string; Input day: 3
begin Input year: 2014
Prepared by:
Mhar Daniel
clrscr;
December 3, 2014_
write('Input month: ');
readln(month);
write('Input day: ');
readln(day);
write('Input year: ');
readln(year);
if(month='1') then
month:='January'
else if(month='2') then
month:='February'
else if(month='3') then
month:='March'
else if(month='4') then
month:='April'
else if(month='5') then
month:='May'
else if(month='6') then
month:='June'
else if(month='7') then
month:='July'
else if(month='8') then
month:='August'
else if(month='9') then
month:='September'
else if(month='10') then
month:='October'
else if(month='11') then
month:='November'
else if(month='12') then
month:='December'
else writeln('Invalid Input!'); writeln;
write('',month,' ',day,', ',year); readln; end.

Code: (case) Output:
uses crt; Input month: 1
var month,day,year:integer; Input day: 1
begin Input year: 1234
clrscr;
January 1, 1234_
write('Input month: '); readln(month);
write('Input day: '); readln(day);
write('Input year: '); readln(year); writeln;
case month of
1 : write('January ',day,', ',year);
2 : write('February ',day,', ',year);
3 : write('March ',day,', ',year);
4 : write('April ',day,', ',year);
5 : write('May ',day,', ',year);
6 : write('June ',day,', ',year);
7 : write('July ',day,', ',year);
8 : write('August ',day,', ',year);
9 : write('September ',day,', ',year);
10 : write('October ',day,', ',year);
11 : write('November ',day,', ',year);
12 : write('December ',day,', ',year);
Prepared by:
Mhar Daniel
0..12345 : write('Invalid Input!');
end; readln; end.

7. Write a program that examines the value of a variable called temp. Then display the following messages, depending on the value
assigned to temp.

Temperature Message

Less than 0 ICE
Between 0 and 100 WATER
Exceeds 100 STEAM

Code: (if else) Output:
uses crt; Input integer: -1
var temp:integer; ICE_
begin clrscr;
write('Input integer: ');
readln(temp);
if(temp<0) then
write('ICE')
else if(temp<=100) then
write('WATER')
else if(temp>100) then
write('STEAM');
readln; end.

Code: (case) Output:
uses crt; Input integer: 200
var temp:integer;
begin STEAM_
clrscr;
write('Input integer: ');
readln(temp); WRITELN;
case temp of
-12345..-1: write('ICE');
0..100: write('WATER');
101..12345: write('STEAM');
end; readln; end.

8. Write a program for the Air Force to label an aircraft as military or civilian. The program is to be given the planes observed speed
in km/h and its estimated length in meters. For Planes traveling in excess of 1100 km/h, and longer than52 meters, you should label
them as civilian aircraft, and shorter such as 500 km/h and 20 meters as military aircraft. For planes traveling at more slower
speeds, you will issue an Its a bird message.

Code: (if else) Output:
uses crt; Input Distance Traveled: 1100
var distance:integer; Civilian Aircraft_
begin clrscr;
write('Input Distance Traveled: ');
readln(distance);
if(distance >= 1100) then
write('Civilian Aircraft')
else if(distance >=500) then
write('Military Aircraft')
else write('It s a bird');
readln; end.

Prepared by:
Mhar Daniel
Code: (case) Output:
uses crt; Input Distance Traveled: 500
var distance:integer; Military Aircraft_
begin clrscr;
write('Input Distance Traveled: ');
readln(distance);
case distance of
1100..12345: write('Civilian Aircraft');
500..1099: write('Military Aircraft');
-12345..499: write('Its a Bird');
end; readln; end.

9. Write a program that determines the class of the Ship depending on its class ID (IDENTIFIER). Here are the criteria. The class ID
serves as the input data and the Ship class as the output info..

Class ID Ship Class

B or b Battleship
C or c Cruiser
D or d Destroyer
F or f Frigate

Code: (if else) Output:
uses crt; Input Class ID(B,C,D OR F): F
var class:char;
begin Frigate
clrscr; _
write('Input Class ID(B,C,D OR F): ');
readln(class); writeln;
if(class='B') or (class='b') then
write('Battleship')
else if(class='c') or (class='C') then
write('Cruiser')
else if(class='d') or (class='D') then
write('Destroyer')
else if(class='f') or (class='F') then
writeln('Frigate')
else write('Invalid Input!'); readln; end.

Code: (case) Output:
uses crt; Input Class ID(B,C,D OR F): f
var class:char;
begin Frigate
clrscr; _
write('Input Class ID(B,C,D OR F): ');
readln(class); writeln;
case class of
'B': write('Battleship');
'b': write('Battleship');
'c': write('Cruiser');
'C': write('Cruiser');
'd': write('Destroyer');
'D': write('Destroyer');
'f': writeln('Frigate');
'F': writeln('Frigate');
'a'..'z': write('Invalid Input!');
'A'..'Z': write('Invalid Input!'); end; readln; end.
Prepared by:
Mhar Daniel

10. The National Earthquake Information Center has the following criteria to determine the earthquakes damage. Here are the
given Richter scale criteria and their corresponding characterization. The Richter scale serves as the input data and the
characterization as output information.
Richter Characterization
Numbers (n)

N<5.0 Little or no damage
5.0>=n<5.5 Some damage
5.5>=n<6.5 Serious damage
6.5>=n<7.5 Disaster
Higher Catastrophe

Code: (if else) Output:
uses crt; Input data: 4.9
var num : real;
begin Little or no damage_
clrscr;
write('Input data: ');
readln(num); writeln;
if(num<5.0) then
write('Little or no damage')
else if(num<5.5) then
write('Some damage')
else if(num<6.5) then
write('Serious damage')
else if(num<7.5) then
write('Disaster')
else if(num>=7.5) then
write('Catastrophe'); readln; end.

11. Write a program that accepts a number and outputs its equivalent in words.

Sample input/output dialogue:
Enter a number: 1380
One thousand three hundred eighty
Take note that the maximum input number is 3000.

12. Write a program that accepts an ordinary number and outputs its equivalent Roman numerals. The ordinary numbers and their
equivalent Roman numerals are given below:

Ordinary Numbers Roman Numerals
1 I
5 V
10 X
50 L
100 C
500 D
1000 M

13. Write a program that computes and assesses the tuition fee of the students in one trimester, based on the given mode of
payment below:

Plan (key) Discount (-) or Interest (+)

Cash (1) 10 % discount
Two-Installment (2) 5 % discount
Prepared by:
Mhar Daniel
Three-Installment (3) 10 % interest

Code: (if else) Output:
uses crt; Enter tuition fee: 20000
var tuition,mode,total:real; (Press 1 for Cash, 2 for Two-installment, 3 for Three-Installment)
begin Enter mode of payment: 1
clrscr;
write('Enter tuition fee: '); Your total tuition fee is: 18000.00_
readln(tuition);
writeln('(Press 1 for Cash, 2 for Two-installment, 3 for Three-Installment)');
write('Enter mode of payment: ');
readln(mode); writeln;
if(mode=1) then begin
total:=(tuition * 0.10); total:=(tuition - total);
write('Your total tuition fee is: ',total:4:2); end
else if(mode=2) then begin
total:=(tuition * 0.05); total:=(tuition - total);
write('Your total tuition fee is: ',total:4:2); end
else if (mode=3) then begin
total:=(tuition * 0.10); total:=(tuition + total);
write('Your total tuition fee is: ',total:4:2); end
else write('Invalid input!..'); readln; end.

14. Write a program that accepts an input grade in percentile form and output its grade equivalent; based on the given range of
percentile and grade equivalent table below:

Range Grade

98-100 1.00
95-97 1.25
92-94 1.50
89-91 1.75
85-88 2.00
82-84 2.25
80-81 2.50
77-79 2.75
75-76 3.00
Other grades Out-of-Range

Code: (if else) Output:
uses crt; Input grade 98
var grade:integer;
begin Grade is 1.00_
clrscr;
write('Input grade ');
readln(grade); writeln;
if(grade >=98) then
write('Grade is 1.00')
else if(grade>=95) then
write('Grade is 1.25')
else if(grade>=92) then
write('Grade is 1.50')
else if(grade>=89) then
write('Grade is 1.75')
else if(grade>=85) then
write('Grade is 2.00')
else if(grade>=82) then
Prepared by:
Mhar Daniel
write('Grade is 2.25')
else if(grade>=80) then
write('Grade is 2.50')
else if(grade>=77) then
write('Grade is 2.75')
else if(grade>=75) then
write('Grade is 3.0')
else write('Out of Range!'); readln; end.

Code: (case) Output:
uses crt; Input grade 74
var grade:integer;
begin Out of Range!_
clrscr;
write('Input grade ');
readln(grade); writeln;
case grade of
98..100: write('Grade is 1.00');
95..97: write('Grade is 1.25');
92..94: write('Grade is 1.50');
89..91: write('Grade is 1.75');
85..88: write('Grade is 2.00');
82..84: write('Grade is 2.25');
80..81: write('Grade is 2.50');
77..79: write('Grade is 2.75');
75..76: write('Grade is 3.0');
0..12345: write('Out of Range!'); end; readln; end.

15. Write a program that calculates and produces these two columns sequence numbers using the three looping
statements:

Sequence nos. Squared
1 1
2 4
3 9 1
st
Solution-using for loop statement
4 16 2
nd
Solution-using repeat until
5 25 3
rd
Solution-using while do

Code: (for to do) Output:
uses crt; Sequence nos. Squared
var sequence:integer;
begin clrscr; 1 1
write('Sequence nos.'); 2 4
write('':5); 3 9
writeln('Squared'); writeln; 4 16
for sequence:=1 to 5 do 5 25
writeln('':5,sequence,' ':15,sequence*sequence); readln; end. _

Code: (repeat until) Output:
uses crt; Sequence nos. Squared
var sequence:integer;
begin clrscr; 1 1
write('Sequence nos.'); 2 4
write('':5); 3 9
writeln('Squared'); writeln; 4 16
Prepared by:
Mhar Daniel
sequence:=1; 5 25
repeat _
writeln('':5,sequence,' ':15,sequence*sequence); inc(sequence); until(sequence>5);
readln; end.

Code: (while do) Output:
uses crt; Sequence nos. Squared
var sequence:integer;
begin clrscr; 1 1
write('Sequence nos.'); 2 4
write('':5); 3 9
writeln('Squared'); writeln; 4 16
sequence:=1; 5 25
while <6 do begin _
writeln('':5,sequence,' ':15,sequence*sequence); inc(sequence); end; readln; end.

16. Write a program which produces the given sequence nos. (in alternate arrangement) using the three looping
statements:

1,5,2,4,3,3,4,2,5,1,
Code: (for to do) Output:
uses crt; Input integers:
var arr:array[1..5] of integer; 5
var num:integer; 4
begin clrscr; 3
writeln('Input integers: '); 2
for num:=1 to 5 do 1
readln(arr[num]); writeln;
for num:=1 to 5 do 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, _
write(arr[num],', ');
for num:=5 downto 1 do
write(arr[num],', '); readln; end.

Code: (repeat until) Output:
uses crt; Input integers:
var arr:array[1..5] of integer; 1
var num:integer; begin 2
clrscr; num:=1; 3
writeln('Input integers: '); repeat 4
readln(arr[num]); inc(num); 5
until(num>5); writeln;
num:=1; repeat 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, _
write(arr[num],', '); inc(num);
until(num>5);
num:=5; repeat
write(arr[num],', '); dec(num);
until(num<1); readln; end.

Code: (while do) Output:
uses crt; Input integers:
var arr:array[1..5] of integer; 9
var num:integer; begin 8
clrscr; num:=1; 7
Prepared by:
Mhar Daniel
writeln('Input integers: '); 6
while num<6 do begin 5
readln(arr[num]);
inc(num); end; 9, 8, 7, 6, 5, 5, 6, 7, 8, 9, _
writeln; num:=1;
while num<6 do begin
write(arr[num],', '); inc(num);
end; num:=5;
while num>0 do begin
write(arr[num],', ');
dec(num); end; readln; end.

17. Write a program which produces the given sequence numbers (in alternate arrangement and reverse order of the
problem no. 2) using the three looping statements:

5,1,4,2,3,3,2,4,1,5

Code: (for to do) Output:
uses crt; Input integers:
var arr:array[1..5] of integer; 5
var num:integer; 4
begin clrscr; 3
writeln('Input integers: '); 2
for num:=1 to 5 do 1
readln(arr[num]); writeln;
for num:=1 to 5 do 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, _
write(arr[num],', ');
for num:=5 downto 1 do
write(arr[num],', '); readln; end.

Code: (repeat until) Output:
uses crt; Input integers:
var arr:array[1..5] of integer; 1
var num:integer; begin 2
clrscr; num:=1; 3
writeln('Input integers: '); repeat 4
readln(arr[num]); inc(num); 5
until(num>5); writeln;
num:=1; repeat 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, _
write(arr[num],', '); inc(num);
until(num>5);
num:=5; repeat
write(arr[num],', '); dec(num);
until(num<1); readln; end.

Code: (while do) Output:
uses crt; Input integers:
var arr:array[1..5] of integer; 9
var num:integer; begin 8
clrscr; num:=1; 7
writeln('Input integers: '); 6
while num<6 do begin 5
Prepared by:
Mhar Daniel
readln(arr[num]);
inc(num); end; 9, 8, 7, 6, 5, 5, 6, 7, 8, 9, _
writeln; num:=1;
while num<6 do begin
write(arr[num],', '); inc(num);
end; num:=5;
while num>0 do begin
write(arr[num],', ');
dec(num); end; readln; end.

18. Write a program that calculates the factorial value of the input number n! Use the incrementation formula (i++) for
your solution instead of decrementation formula (i--).

Sample input/output dialogue:

Enter a no. 4
Factorial value: 24

Code: (for to do) Output:
uses crt; Input num: 4
var num,a,b:integer; Factorial value: 24_
begin
clrscr;
write('Input num: ');
readln(num); b:=1;
for a:=1 to num do
b:=b * a;
write('Factorial value: ',b); readln; end.

Code: (repeat until) Output:
uses crt; Input num: 4
var num,a,b:integer; Factorial value: 24_
begin clrscr;
write('Input num: ');
readln(num);
b:=1; a:=1;
repeat b:=b * a;
inc(a);
until (a>num);
write('Factorial value: ',b); readln; end.

Code: (while do) Output:
uses crt; Input num: 4
var num,a,b:integer; Factorial value: 24_
begin clrscr;
write('Input num: ');
readln(num); b:=1; a:=1;
while a<=num do begin
b:=b * a; inc(a); end;
write('Factorial value: ',b); readln; end.

19. Write a program that generates and displays the Fibonacci sequence numbers of n (as input).
Prepared by:
Mhar Daniel

Sample input/output dialogue:

Enter a no. 9
Fibonacci series: 1 1 2 3 4 8 13 21 34

Code: (for to do) Output:
uses crt; Enter a no. 9
var no,no1,no2,result,a:integer; Fibonacci series: 1 1 2 3 4 8 13 21 34_
begin clrscr;
write('Enter a no. ');
readln(no);
no1:=1; no2:=1;
write('Fibonacci series: ',no1,' ',no2);
for a:=1 to no-2 do begin
result:=no1+no2; write(' ',result);
no1:=no2; no2:=result; end; readln; end.

Code: (repeat until) Output:
uses crt; Enter a no. 9
var no,no1,no2,result,a:integer; Fibonacci series: 1 1 2 3 4 8 13 21 34_
begin clrscr;
write('Enter a no. ');
readln(no);
no1:=1; no2:=1;
write('Fibonacci series: ',no1,' ',no2);
a:=1; repeat
result:=no1+no2; write(' ',result);
no1:=no2; no2:=result; inc(a);
until(a>9-2); readln; end.

Code: (while do) Output:
uses crt; Enter a no. 9
var no,no1,no2,result,a:integer; Fibonacci series: 1 1 2 3 4 8 13 21 34_
begin clrscr;
write('Enter a no. ');
readln(no);
no1:=1; no2:=1;
write('Fibonacci series: ',no1,' ',no2); a:=1;
while a<no-1 do begin
result:=no1+no2; write(' ',result);
no1:=no2; no2:=result;
inc(a); end; readln; end.

20. Write a program to scan a number n and then output the sum of the square from 1 to n. Thus, if the input is 4, the
output should be 30 because:

1
2
+ 2
2
+ 3
2
+ 4
2
1 + 4 + 9 + 16 = 30

Code: (for to do) Output:
uses crt; Input number: 4
var num,a,pow,sum:integer;
Prepared by:
Mhar Daniel
begin clrscr; Total sum of squared is 30_
write('Input number: ');
readln(num);
for a:=1 to num do
begin pow:=(a*a);
sum:=(sum+pow); end; writeln;
write('Total sum squared is ',sum); readln; end.

Code: (repeat until) Output:
uses crt; Input number: 4
var num,a,pow,sum:integer;
begin clrscr; Total sum of squared is 30_
write('Input number: ');
readln(num); a:=1;
repeat pow:=(a*a);
sum:=(sum+pow); inc(a);
until(a>num); writeln;
write(' Total sum of squared is ',sum); readln; end.

Code: (while do) Output:
uses crt; Input number: 4
var num,a,pow,sum:integer;
begin clrscr; Total sum of squared is 30_
write('Input number: ');
readln(num); a:=1;
while(a<=num) do begin pow:=(a*a);
sum:=(sum+pow);
inc(a); end; writeln;
write(' Total sum of squared is ',sum); readln; end.

21. Write a program to calculate the sum of the sequence no. from 1 to n. Thus the input is 6, the output should be 21
because: 1 + 2 + 3 + 4 + 5 + 6 = 21.

Code: (for to do) Output:
uses crt; Input number: 6
var number,a,sum:integer;
begin Total sum is 21_
clrscr;
write('Input number: ');
readln(number); writeln;
for a:=1 to number do
sum:=sum+a;
write('Total sum is ',sum); readln; end.

Code: (repeat until) Output:
uses crt; Input number: 6
var number,a,sum:integer;
begin Total sum is 21_
clrscr;
write('Input number: ');
readln(number);
writeln; a:=1;
repeat
sum:=sum+a; inc(a);
Prepared by:
Mhar Daniel
until(a>number);
write('Total sum is ',sum); readln; end.

Code: (while do) Output:
uses crt; Input number: 6
var number,a,sum:integer;
begin Total sum is 21_
clrscr;
write('Input number: ');
readln(number); writeln; a:=1;
while a<=number do begin
sum:=sum+a; inc(a); end;
write('Total sum is ',sum); readln; end.

22. Write a program that reverses the input number n. Formulate an equation come up with the answer:

Sample input/output dialogue:

Enter a no. 1238
Reverse no. 8321

Code: (repeat until) Output:
uses crt; Enter a no. 123
var num,reverse:integer;
begin clrscr; Reverse no. 321_
write('Enter a no. ');
readln(num);
writeln; repeat
reverse:=reverse * 10;
reverse:=reverse + (num mod 10);
num:=num div 10;
until(num<1);
write('Reverse no. ',reverse); readln; end.

Code: (while do) Output:
uses crt; Enter a no. 123
var num,reverse:integer;
begin clrscr; Reverse no. 321_
write('Enter a no. ');
readln(num); writeln;
while num > 0 do begin
reverse:=reverse * 10;
reverse:=reverse + (num mod 10);
num:=num div 10; end;
write('Reverse no. ',reverse); readln; end.

23. Write a program to scan a no. n and then output the sum of the power from 1 to n. Thus, if the input is 4, the output
should be 288 because: 1
1
+ 2
2
+ 3
3
+ 4
4
= 1 + 4 + 27 + 256 =288.

S-ar putea să vă placă și