pascal act

Upload: mhar-daniel

Post on 03-Jun-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 Pascal Act

    1/15

    Prepared by

    Mhar Danie

    TPL ACT.

    (MIDTERM PROJECT)

    SUBMITTED BY: SUBMITTED TO:

    MHAR DANIEL MR. IAN CLINT E. OFFEMARIA

  • 8/11/2019 Pascal Act

    2/15

    Prepared by

    Mhar Danie

    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: '); 78readln(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(bigc) then small:=c;

    if(bigd) then small:=d;

    if(bige) then small:=e;

    if(big

  • 8/11/2019 Pascal Act

    3/15

    Prepared by

    Mhar Danie

    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.

    1st

    Solution: Use the ladderized if/else if /else conditional statements

    2nd

    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

    1st

    Solution: Use the ladderized if/else if /else conditional statements

    2nd

    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

  • 8/11/2019 Pascal Act

    4/15

    Prepared by

    Mhar Danie

    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);

  • 8/11/2019 Pascal Act

    5/15

    Prepared by

    Mhar Danie

    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 WATERExceeds 100 STEAM

    Code: (if else) Output:

    uses crt; Input integer: -1

    var temp:integer; ICE_

    begin clrscr;

    write('Input integer: ');

    readln(temp);

    if(temp= 1100) then

    write('Civilian Aircraft')

    else if(distance >=500) then

    write('Military Aircraft')

    else write('It s a bird');

    readln; end.

  • 8/11/2019 Pascal Act

    6/15

    Prepared by

    Mhar Danie

    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.

  • 8/11/2019 Pascal Act

    7/15

    Prepared by

    Mhar Danie

    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=n=n=n

  • 8/11/2019 Pascal Act

    8/15

    Prepared by

    Mhar Danie

    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.5077-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

  • 8/11/2019 Pascal Act

    9/15

  • 8/11/2019 Pascal Act

    10/15

    Prepared by

    Mhar Danie

    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 1write('Sequence nos.'); 2 4

    write('':5); 3 9

    writeln('Squared'); writeln; 4 16

    sequence:=1; 5 25

    while 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

  • 8/11/2019 Pascal Act

    11/15

    Prepared by

    Mhar Danie

    writeln('Input integers: '); 6

    while num5); 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

  • 8/11/2019 Pascal Act

    12/15

    Prepared by

    Mhar Danie

    readln(arr[num]);

    inc(num); end; 9, 8, 7, 6, 5, 5, 6, 7, 8, 9, _

    writeln; num:=1;

    while num0 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

  • 8/11/2019 Pascal Act

    13/15

    Prepared by

    Mhar Danie

    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

  • 8/11/2019 Pascal Act

    14/15

    Prepared by

    Mhar Danie

    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: 4var num,a,pow,sum:integer;

    begin clrscr; Total sum of squared is 30_

    write('Input number: ');

    readln(num); a:=1;

    while(a

  • 8/11/2019 Pascal Act

    15/15