Vous êtes sur la page 1sur 17

Basic Input / Output

Write a program that calculates the sum of two input numbers.


Algorithm
Input- Enter two numbers (N1,N2)
Process- Compute the sum (Sum=N1+N2)
Output- Display the sum
Program
Program Add
Uses CRT;
VAR
N1, N2: Integer;
Sum : Integer;
BEGIN
CLRSCR;
WriteLN (Enter Two Numbers);
ReadLN (N1, N2);
Sum: =N1+N2;
WriteLN (`THE SUM: SUM`);
ReadLN;
End.
OUTPUT
Enter Two Numbers

Example
2
5+5
THE SUM
is 10
Write
a program
to compute the area of a circle use the formula
2
A=r

Where Pi=3.1416
Algorithm
Input- Enter Radius (R)
Process- Compute Area
(A=Pi*r*r)
Output- Display A
Program
Program Area;
Uses CRT;
CONST
Pi=3.1416;
VAR
R: Integer;
A: Real;
BEGIN
CLRSCR;
WriteLN (`Enter Radius`);
ReadLN (R);
A=Pi*r*r;
WriteLN (`THE AREA , A: 8,2`);
ReadLN;
End.

Output

Enter Radius
1
The Area: Pi=3.1416

Short If Then-Else
Write a program that determines if the input numbers is
equivalent to the magic words `ILOVEYOU` if not. Display the
result `ILOVEYOU` if the input is 143 and `WRONG GUESS` if it is
not.
Algorithm
Enter Your Guess Number (N)
IF (N=143) then
WriteLN (`143 is ILOVEYOU`)
ELSE
WriteLN (`WRONG GUESS`)
PROGRAM
Program Magic Number;
USES CRT;
VAR
N: INTEGER;
BEGIN
CLRSCR;
WriteLN (`ENTER YOUR GUESS NUMBER`)
ReadLN (N);
If (N=143) then

WRITELN (`143 is ILOVEYOU`)


ELSE
WriteLN (`Wrong Guess`);
ReadLN;
End.

OUTPUT
Enter Your Guess Number
143
143 is ILOVEYOU

Example 2
Write a program that determines if the input number is positive or
negative consider 0 as positive number.
ALGORITHM
Enter a number (N)
IF (N>=0) then
WriteLN (`POSITIVE`)
ELSE
WriteLN (`NEGATIVE`)

PROGRAM
Program POSNEG;
Uses CRT;

VAR
N: Integer;
Begin
CLRSCR
WRITELN (`ENTER YOUR NUMBER`)
READLN (N);
IF (N>=0) then
WriteLN (`POSITIVE`)
Else
WriteLN (`NEGATIVE`);
ReadLN;
End.
OUTPUT
Enter Your Number
5
Positive

LONG IF-THEN-ELSE
Write a program that will assist a teacher in calculating student
grades at the END of the semester it accepts a numerical grade
as input then it will display the character grade as output based
on the given scale.
Range

Grade

90 and above

80-89

70-79

60-69

Below 60
ALGORITHM
Enter Grade (G)
If (G>=90) then
WriteLN (`A`)
Else if (G>=80) then
WriteLN (`B`)
Else if (G>=70) then
WriteLN (`C`)
Else if (G>=60) then
WriteLN (`D`)
Else
WriteLN (`F`)

Program
Program Grade;
Uses CRT;
VAR
G: Integer;

BEGIN
WriteLN (`Enter Grade`);
ReadLN (G);
IF (G>=90) then
WriteLN (`A`)
Else if (G>=80) then
WriteLN (`B`)
Else if (G>=70) then
WriteLN (`C`)
Else if (G>=60) then
WriteLN (`D`)
Else
WriteLN (`F`);
ReadLN;
End.
OUTPUT
ENTER GRADE
70
Your Grade2is C
Example

Write a program that displays an equivalent color once an input


letter matches its first character for example b for BLUE, r for RED
and so on. Here is the given criteria.
Letters

Colors

B or b

Blue

R or r

Red

G or g

Green

Y or y

Yellow

Other color

unknown color

ALGORITHM
Enter a letter (L)
If (L=`B`) or (L=`b`) then
WriteLN (`BLUE`)
If (L=`R`) or (L=`r`) then
WriteLN (`Red`)
If (L=`G`) or (L=`g`) then
WriteLn (`Green`)
If (L=`Y`) or (L=`y`) then
WriteLN (`Yellow`)
Else
WriteLN (`Unknown Color`);

Program
Program Color;
Uses CRT;
VAR
L: Char;
BEGIN
CLRSCR;

WriteLN (`Enter Color`);


ReadLN (C);
IF (L=`B`) or (L=`b`) then
WriteLN (`BLUE`)
Else if (L=`R`) or (L=`r`) then
WriteLN (`RED`)
Else if (L=`G`) or (L=`g`) then
WriteLN (`GREEN`)
Else if (L=`Y`) or (L=`y`) then
WriteLN (`YELLOW`)
Else
WRITELN (`UNKNOWN COLOR`);
ReadLN;
End.
OUTPUT
Enter color
R
Color is Red

Case Conditional Statement

Write a program to assist a teacher in calculating grade.


Range
90-100

Grade
A

80-89

70-79

60-69

50-59
Others

F
act of range

ALGORITHM
Enter Grade (G)
Case G of
90..100: WriteLN (`A`);
80..89: WriteLN (`B`);
70..79: WriteLN (`C`);
60..69: WriteLN (`D`);
50..59: WriteLN (`F`);
Else
WriteLN (`Out of Range`);
End;

Program
Program;
Uses CRT;
VAR
G: Integer;
BEGIN
CLRSCR;
WriteLN (`Enter Grade`);

ReadLN (`G`);
Case of G
90.100: WriteLN (`A`);
80..89: WriteLN (`B`);
70..79: WriteLN (`C`);
60..69: WriteLN (`D`);
50..59: WriteLN (`F`);
Else
WriteLN (`OUT OF ORDER`);
End;
ReadLN;
End.
OUTPUT
Enter A Grade
85
Your Grade2is B
Example

Write a program that displays an equivalent color.


Letters

Color

`B` or `b`

Blue

`R` or `r`

Red

`G` or `g`

Green

`Y` or `y`

Yellow

Other color
ALGORITHM

Unknown Color

Enter a Letter (L)


If (L=`B`) or (L=`b`) then
WriteLN (`BLUE`)
If (L=`R`) or (L=`r`) then
WriteLN (`RED`)
If (L=`G`) or (L=`g`) then
WriteLN (`GREEN`)
If (L=`Y`) or (L=`y`) then
WriteLN (`Yellow`)
ELSE
WRITELN (`Unknown Color`);

PROGRAM
Program Color;
Uses CRT;
VAR
L: Char;
BEGIN
CLRSCR;
WriteLN (`Enter Color`);
ReadLN (C);

If (L=`B`) or (L=`b`) then


WriteLN (`BLUE`)
Else if (L=`R`) or (L=`r) then
WriteLN (`RED`)
Else if (L=`G`) or (L=`g`) then
WriteLN (`GREEN`)
Else if (L=`Y`) or (L=`y`) then
WriteLN (`YELLOW`)
Else
WriteLN (`UNKNOWN COLOR`);
ReadLN;
End.
Output
Enter Color
G

Looping Conditional Statement

Color is
Write
a Green
program that generates the given sequence numbers.
1
2
3
4
5

ALGORITHM
N: =1
While (N<=5) do

Begin
WriteLN (N);
N=N+1
End.
PROGRAM
Program Sequence 1;
USES CRT;
VAR

N: INTEGER;

Begin
CLRSCR;
WRITELN (`WHILE DO LOOP-INCREMENTATION`);
N: =1;
While (N<=5) DO
BEGIN
WRITELN (N);
Delay (140000);
END;
READLN;
END.

PROGRAM SEQUENCE 2;
USES CRT;
VAR N: Integer;
CLRSCR;

WriteLN (`FOR DO LOOP-INCREMENTATION`);


FOR N: =1 TO 5 DO
BEGIN
WRITELN (N);
DELAY (19000);
END;
READLN;
END.

Example 2
Write a program that generates the given sequence numbers
5
4
3
2
1

ALGORITHM
N: =5;
WHILE (N>=1) DO
BEGIN
WRITELN (N);

N:=N-1:
END.
PROGRAM
Program Sequence 1;
Uses CRT;
VAR N: INTEGER;
BEGIN
CLRSCR;
WRITELN (`WHILE DO LOOP-DECREASE`);
N: =5:
WHILE (N>=1) DO
BEGIN
WRITELN (N);
N: =N-1;
DELAY (19000);
END;
READLN;
END.

PROGRAM SEQUENCE 2;
USES CRT;
VAR N: INTEGER;
BEGIN
CLRSCR;

WRITELN (`FOR DO-LOOP-DECREMENTATION`);


FOR N: =5 DOWN TO 1 DO
BEGIN
WRITELN (N);
DELAY (19000);
END;
READLN;
END.

Vous aimerez peut-être aussi