Elementary Programming Concepts
Elementary Programming Concepts
Programming language : A combination of restricted vocabulary (words and symbols) and a syntax (rules ) is called a programming language.
A programming language is a medium used by the users to communicate and write instruction for a computer.
Types of programming languages:
There are various types of programming languages that can be categorized into four types:
i) High level programming language (e.g. Qbasic, COBOL etc.)
ii) Middle level programming language ( e.g. C )
iii) Low level programming language (e.g. Assembly language)
iv) Machine language (e.g. instructions written using 0’s and 1’s )
Programming – the process of writing instructions for computer by using computer programming language is called as programming or coding.
Program – A set or collection of sequential instruction grouped together to perform a specific task is called a program.
Software – a set or collection of various and similar programs makes a software.
PROGRAMING LANGUAGE
INSTRUCTION
PROGRAM
SOFTWARE
Programmer – Computer programmers are responsible for designing, writing and modifying computer programs. Programmers can be classified into two categories:
a) System programmer
b) Application programmer
Tools for designing a program:
Algorithm – A set sequential steps that specify the solution to a given problem is called an algorithm.
Flowchart – Symbolic visual representation of an algorithm is called a flowchart.
Advantages of flowchart:
a) Representing algorithm by flowchart and than converting it to computer program is easier and accurate than writing the program directly.
b) Flowcharts are independent of any programming languages. Hence the algorithm given in a flowchart can be translated into more than one programming languages.
Characters used in Qbasic :
a) Alphabetic characters ( A to Z )
b) Numeric characters ( 0 to 9 )
c) Special characters (“” , ; ,! etc.)
d) Function keys ( F1 to F12 )
Elementary data types used in Qbasic :
Qbasic supports two data types represented by variable which are :
a) Numeric
b) String
Variable – a symbol whose value changes during the program execution is called a variable.
For example in x = y + z ( the value of x varies depending upon the values of y and z.)
Numeric variable – A symbol that is used to store a number is known as numeric variable.
For example a= 9 and b =1.5
Types of numeric variable :
A numeric variable can further be broken down to
a) Integer (%)
b) Long integer (&)
c) Single precision (!)
d) Double precision (#)
String variable – A symbol or variable used to store string or strings is called a string variable.
The name of a string variable should start with a character and should end with a dollar ($) sign.
For example K$= “KATHMANDU”
N$ =”NEPAL”
String – A group of letters or numbers or both which are enclosed within a pair of quotation marks ( “ “ ) is called a string.
For example “ BVS “
“ 123 BVS“
Constant- A symbol whose value does not change during the execution of the program is called a constant.
Types of constant : i) numeric constant ii) string constant
Types of expression :
a) Arithmetic expression – An expression which contains values, variables and operators that returns a single numeric value after operation is called arithmetic expression. For example let x= y+z
b) Logical expression – A logical expression contains values and operations which returns one of two logical values ( true or false ) for example 6>5<7
c) String expression – an expression which returns string value after processing strings with the help of string operators. For example PRINT A$ + B$
Operators- Operators are symbols representing valid operations on values.
Operand – Operands may be a constant value or variable on which mathematical and logical operations take place.
The following expression shows operands with an operator.
OPERATOR
X = 7 + 9
OPERANDS
Types of operators:
a) Arithmetic operators
b) Relation operators
c) Logical operators
d) String operators
Arithmetic operators – Arithmetic operators are used to solve the value of an expression. In Qbasic, the following arithmetic operators can be used.
Operators | Action |
+ | Addition |
- | subtraction |
* | multiplication |
/ | Division |
^ | To the power |
Relational operators – Relational operators are used to compare two or more values. Using this, we can compare values of variables with a constant. In Qbasic the following relational operators can be used.
Operators | Relation |
= | Equal to |
<> | Not equal to |
< | Less than |
<= | Less than equal to |
> | Greater than |
>= | Greater than equal to |
Logical operators – Logical operators are used to examine two or more relations. The outcome of this examination is given in TRUE or FALSE. Logical operators most often used are AND, OR, and NOT.
Statement – a collection of commands used in the lines of a program is called a statement. In Qbasic, the statements can be divided into four groups :
a) Assignment statement
b) Input / output statement
c) Declaration statement
d) Control statement
a) Assignment statement – A statement used to assign value of a variable is called assignment statement.
For example let A =5
B = 7
D = A +B
PRINT D
END
SWAP statement – SWAP statement is used to exchange the values of two similar type of variables
Syntax – SWAP <variable 1><varaible2>
For example:
REM EXAMPLE OF SWAP
LET X = 100
LET Y = 200
PRINT “BEFORE SWAPPING DATA “;
PRINT X
PRINT Y
PRINT “AFTER SWAPPING DATA” ;
SWAP X , Y
PRINT X
PRINT Y
END
b) Input / output statement – Input / output statements allow the user to input data to the computer and print the result after processing. They are used to perform input/output operations of a computer. Some common input/output statements are INPUT, LINE INPUT, PRINT, LPRINT etc.
CLS
Function – Clears the screen.
INPUT
Function – Reads input from the keyboard.
DATA
Function – Specifies values to be read by subsequent READ statements.
READ
Function – Reads those values and assigns them to variables.
RESTORE
Function – Allows READ to reread values in specified DATA statements.
TAB
Function – Moves the cursor to a specified print position
Function – Writes data to the screen or to a file.
Declaration statement – A statement used to define or declare a constant, variable or array etc, is called declaration statement.
CONST, DIM, REM
CONST
Function – declares one or more symbolic constants.
DIM
Function – declares an array or specifies data type for a non array variable.
REM
Function – Allows explanatory remarks to be inserted in a program.
Example of CONST and REM
REM TO CALCULATE AREA OF A CIRCLE
CLS
CONST PI =3.141593
INPUT “RADIUS OF CIRCLE”; R
PRINT “AREA = “ PI * R ^ 2
END
EXAMPLE OF DIM
DIM NAME$ (5)
FOR I = 1 TO 5
READ NAME$ (I)
PRINT NAME$ (I)
NEXT I
DATA NEPAL,INDIA,CHINA,BHUTAN,SRILANKA
END
Control flow statements – A statement which controls the program flow while executing the program instructions one after another is called control statement.
GOTO, IF …. END IF, FOR ….. NEXT etc. are few examples of control statements.
GOTO LINE
Function – Branches to a specified line.
Decision making statements:
1. IF ….. THEN …. END IF
Function – This statement is used for making decisions and it also checks a single condition.
Syntax – IF <CONDITION> THEN
<STATEMENTS>
END IF
EXAMPLE: CLS
LET C = 1
TOP :
PRINT C
LET C = C + 1
IF C < = 10 THEN
GOTO TOP
END IF
END
2. IF …… THEN ….. ELSE ….. END IF
Function – This statement is used for making decisions and it checks double condition.
Syntax – IF <CONDITION> THEN
<STATEMENT>
ELSE
<STATEMENT>
END IF
Example : CLS
INPUT “ENTER LENGTH”; L
INPUT “ENTER BREADTH “; B
LET A = L * B
IF A > = 50 THEN
PRINT “BIG”
ELSE
PRINT “SMALL”
END IF
END
3. IF …. THEN …. ELSE IF
Function – This statement is used for checking multiple conditions
Syntax – IF <CONDITION> THEN
<STATEMENTS>
ELSE IF
<STATEMENTS>
ELSE
<STATEMENTS>
END IF
EXAMPLE ;
CLS
INPUT “ENTER PERCENTAGE “; P
IF P > = 60 THEN
PRINT “ 1ST. DIVISION”
ELSE IF P >= 50 AND P < = 59 THEN
PRINT “2ND DIVISION”
ELSE IF P > = 40 AND P < = 49 THEN
PRINT “3RD DIVISION”
ELSE
PRINT “FAIL “
END IF
END
4. SELECT CASE …… END SELECT
Function – This command executes one of several statement blocks depending on the value of the expression.
Syntax : SELECT CASE TEXT EXPRESSION
CASE EXPRESSION LIST ONE
<STATEMENT BLOCK 1>
CASE EXPRESSION LIST2
<STATEMENT BLOCK 2 >
CASE ELSE
<STATEMENT BLOCK 3 >
END SELECT
EXAMPLE :
CLS
INPUT “ENTER NUMBER FROM 1 TO 10 “; N
SELECT CASE N
CASE 1,3,5,7,9
PRINT “THE NUMBER IS ODD”
CASE 2,4,6,8,10
PRINT “THE NUMBER IS EVEN”
CASE ELSE
PRINT “INVALID NUMBER “
END SELECT
END
5. FOR ….. NEXT
Function – Repeats a block of statements a specified number of times.
Syntax - FOR <VARIABLE > = < 1 TO N ><STEP >
STATEMENTS
NEXT
EXAMPLE – FOR I = 1 TO 10
PRINT I
NEXT
END
6. WHILE …. WEND
Function – This command executes a series of statements as long as the specified condition is true.
Syntax - WHILE <CONDITION>
STATEMENTS
WEND
EXAMPLE –
CLS
X = 1
WHILE X < = 10
PRINT X
X = X + 1
WEND
END
7. DO …. LOOP
Function – This command repeats a block of statements while the condition is true or until the condition becomes true.
Syntax - DO <WHILE /UNTIL > CONDITION
STATEMENTS
LOOP
EXAMPLE –
X = 1
DO WHILE X < = 10
PRINT X
X = X + 1
LOOP
END
Write a program to generate the following series 2,2,4,6,10 up to 10th term using WHILE … WEND.
REM USING WHILE …. WEND
CLS
A = 2
B = 2
X =1
C = 0
PRINT A,B
WHILE X < = 15
C = A + B
PRINT C
SWAP A , B
SWAP B , C
X = X + 1
WEND
END
QBASIC – QBASIC stands for Quick Beginners All Purpose Symbolic Instruction Code. It is the most popular high level programming language used by the beginners to write and develop elementary programs.
The original BASIC programming language was developed by Thomas Kurtz and John Kemeny in the year 1963 -1964.
Loop – In programming repeated execution of a sequence of statements is called a loop or looping.
Nested loop – A loop within a loop is called a nested loop.
Termination – Ends or terminates a basic program or a procedure.
Bug – An error or fault present in a program is called a bug.
De-bug (debugging) – The process of finding and correcting the bug (error) in a program is called debugging.
Counter – This is a variable in which the count of number of occurrences of certain event is stored while executing the program.
Accumulator – Any variable in which results of mathematical calculations are stored while the execution of the program goes on is called accumulator.
Indentation – Indentation should be followed while writing computer programs. This helps in understanding the program command, minimizes bugs and makes the debugging easy.
Branching (jumping) – If the execution of any program depart conditionally or unconditionally from its sequential flow depending on the result of a test, it is called branching.
String operators / manipulators functions - String represents alpha numeric characters (combination of alphabets and numbers that are enclosed within quotation marks).
Qbasic has many string functions that can be applied to strings and to modify and alter them if necessary. These are called string a manipulators. Since, they manipulate or re-arrange values within a string.
3. LEFT$
Function – Returns a specified number of leftmost characters in a string.
Syntax – LEFT$ ( string expression $, n%)
4. RIGHT$
Function – Returns a specified number of rightmost characters in a string.
Syntax – RIGHT$ ( string expression $, n%)
EXAMPLE :
REM EXAMPLE OF LEFT$ AND RIGHT$
SUB$ = “COMPUTER SCIENCE”
PRINT LEFT$ (SUB$, 8)
PRINT RIGHT$ (SUB$, 7)
END
5. MID$
Function – The MID$ function returns part of a string (a substring)
Syntax – MID$ (string expression$, start %, length %)
EXAMPLE :
REM EXAMPLE OF MID$
TOPIC$ = “COMPUTER IS FUN”
PRINT MID$ (TOPIC$, 1, 8)
PRINT MID$ (TOPIC$, 13, 3)
PRINT MID$ (TOPIC$, 4, 3)
END
6. LEN
Function – Returns the number of characters in a string or the number of bytes required for storing a variable.
Syntax – LEN (string expression $)
EXAMPLE :
REM EAMPLE OF LEN
SUB$ = “COMPUTER SCIENCE”
PRINT LEN (SUB$)
ANUM = 2345.45
PRINT LEN (ANUM)
END
9. STR$
Function – Returns a string representation of a number.
Syntax – STR$ (numeric expression)
10 VAL
Function – Converts a string representation of a number to a number
Syntax – VAL (string expression$)
EXAMPLE :
REM EXAMPLE OF STR$ AND VAL
PRINT “DECIMAL 65 IS REPRESENTED IN HEXADECIMAL AS”;
PRINT “8H” + LTRIM$ (STR$ (41))
PRINT VAL (RIGHT$ (“NEW YEAR”, 4))
END
13. LCASE$
Function – converts strings to all lowercase letters.
Syntax - LCASE$ (STRING EXPRESION $)
14. UCASE$
Function – converts strings to all uppercase letters.
Syntax – UCASE$ (STRING EXPRESSION$)
EXAMPLE :
REM EXAMPLE OF LCASE$ AND UCASE$
TEXT$ = “ KATHMANDU “
PRINT LCASE$ (TEXT$)
PRINT UCASE$ (TEXT$)
END
Mathematical Calculation Functions
1. ATN
Function – ATN returns the arctangent of a specified numeric expression.
Syntax – ATN ( numeric expression )
2. COS
Function – COS returns the cosine of a specified angle.
Syntax – COS (angle)
3. SIN
Function – SIN returns the sine oaf a specified angle
Syntax – SIN (ANGLE)
4. TAN
Function – TAN returns the tangent of a specified angle.
Syntax – TAN (angle)
EXAMPLE:
REM EXAMPLE OF ATN, COS, SIN AND TAN
CLS
CONST PI = 3.1415926
PRINT ATN (TAN(PI/4!)), PI/4!
PRINT (COS (180 * (PI / 180)))
PRINT (SIN ( 90 * ( PI /180 )))
PRINT ( TAN (45 * (PI / 180 )))
END
5. MOD
Function – Divides one number by another and returns the remainder.
Syntax – numeric expression 1 mod numeric expression 2
EXAMPLE :
REM EXAMPLE OF MOD
PRINT 27 MOD 4.6
END
6. SQR
Function – Returns the square root of a numeric expression
Syntax – SQR (numeric expression)
7. ABS
Function – ABS returns absolute value of a number.
Syntax – ABS (numeric expression)
8. SGN
Function – SGN returns absolute a value indicating the sign of a numeric expression.
Syntax – SGN (numeric expression)
EXAMPLE
REM EXAMPLE OF ABS/SGN
PRINT SQR (9)
PRINT ABS (55.5-100!)
PRINT SGN (15), SGN (-12),SGN (0)
END
11. FIX
Function – FIX truncates a floating point expression to its integer position.
Syntax – FIX (numeric expression)
12.INT
Function – INT returns the largest integer less than or equal to a numeric expression.
Syntax – INT (numeric expression)
EXAMPLE:
REM EXAMPLE OF FIX / INT
PRINT FIX (100.34), FIX (-37.25), FIX(.8755)
PRINT INT (8.952),INT(1.9),INT(-7.001)
END
CONCEPT OF MODULAR PROGRAMMING
FUNCTION:
A function in QBASIC is a readymade program or user made (small) program which helps to perform a specific task.
TYPES OF FUNCTIONS:
QBASIC supports two types of function:
a) User defined function
b) Library function
a) User defined function:
A user defined function is created by a user whenever the user needs to perform a certain task that cannot be performed by using a library function. ( a user defined function is created by using FUNCTION ….. END FUNCTION statement)
b) Library function:
Library functions are the ready- made programs that accept data and returns a value.
These functions are written by the developer of QBASIC at the time of development of QBASIC. Library functions are also called routine functions or built-in-functions.
The two commonly used library functions are:
i) String functions/manipulators (e.g. LEN, RIGHT$, MID$ etc)
ii) Mathematical functions (e.g. ABS, INT, MOD etc)
Linear programming:
When a program becomes complex and unmanageable, this type of programming structure is called linear programming.
Modular programming:
The way of writing a program by breaking the complex program into small, logical and manageable part is known as modular programming.
Module/Procedure:
The small logical and manageable part of the program is called a module or procedure, which sometimes are also referred as a sub program or a sub module, that contains codes i.e. instructions for performing a certain task.
(since the modular programming uses small block of functional codes it is also called structured programming)
Advantages of procedure/module:
a) A procedure can be reused more than once without rewriting it, so it reduces the length of a program
b) The procedures can be tested and debugged separately.
c) Improves the readability of the program.
Note : A modular program has a main module and may have many sub-modules. The main module contains the entry and ending point of the program. The statements or codes written in a main module are known as module level codes.
Concept of passing parameters/arguments to a function or procedure:
There are two ways to pass parameters to a function or procedure. They are:
a) Passing by value
b) Passing by reference
Concept of local and global variable
Local variable: A variable which is declared inside a module and which cannot be accessed by all the modules is known as local variable.
( All variables that is declared implicitly or explicitly without using SHARED attribute are local variables )
Global variable: A variable in main module which can be accessed from any sub module or a procedure of a program is known as global variable.
( a global variable is declared in the main module by using DIM or COMMON statement with SHARED attribute )
Example: COMMON SHARED <VARIABLE LIST>
DIM SHARED <VARIABLE LIST>
Types of procedure:
There are two types of procedure:
a) FUNCTION procedure
b) SUB procedure
Creating Function Procedure
a) Function-procedure:
There are many in built or library functions such as RIGHT$, LEN, MID$, MOD, INT etc. available in QBASIC. But at times they are not sufficient for programmers. So, in some cases, we have to make such function ourselves. This type of function is called user-defined function because it is created by the user.
A FUNCTION procedure performs the specific task and returns a value to the main program.
A user-defined function is written with FUNCTION … END FUNCTION statement.
Syntax – FUNCTION NAME <PARAMETERLIST>
< STATEMENTS >
NAME = <EXPRESION>
END FUNCTION
Declaration part
DECLARE:
Function-Declares a FUNCTION or SUB procedure and invokes argument, data type checking.
Syntax – DECLARE FUNCTION NAME (PARAMETER LIST)
NAME = Where NAME is a name of a FUNCTION procedure.
Parameter list: parameter list is the list of variables that accepts the values, passes to it when the function is called. Each variable or constant is separated by commas.
Expression: Expression is the value that has to be returned to the calling module.
Example 1. Program to find the sum of two values using FUNCTION …… END FUNCTION
DECLARE FUNCTION SUM ( A, B )
CLS
INPUT “ ENTER THE FIRST VALUE”; A
INPUT “ENTER THE SECOND VALUE”;B
S = SUM (A,B)
PRINT” THE SUM OF TWO VALUES”; S
END
FUNCTION SUM (A,B)
X= A+B
SUM = X
END FUNCTION
Example 2. Program to find the average of two values
DECLARE FUNCTION AVG ( X, Y )
CLS
INPUT “ ENTER THE FIRST VALUE”; X
INPUT “ENTER THE SECOND VALUE”;Y
AV = AVG (X,Y)
PRINT” THE AVERAGE OF TWO VALUES”; AV
END
FUNCTION AVG (X,Y)
A= (X+Y)/2
AVG = A
END FUNCTION
Creating Function Procedure
b) SUB Procedure:
A SUB procedure is a small logical and manageable part of a program which performs the specific task and does not return any value. A SUB procedure is called by using the CALL statement.
A SUB procedure is defined by using SUB ……. END SUB statement.
SUB …….. END SUB
Function : The SUB statement is used to define a sub procedure.
Syntax: SUB NAME <PARAMETERLIST>
STATEMENT BLOCK
END SUB
CALL:
Function: Transfers control to a SUB procedure.
Syntax : CALL NAME <PARAMETERLIST>
Example 1. Program to find the sum of two values using SUB …… END SUB
REM PARAMETER PASSING BY REFERENCE
DECLARE SUB SUM (A,B)
CLS
INPUT “ENTER FIRST VALUE”;A
INPUT”ENTER SECOND VALUE”;B
CALL SUM (A,B)
END
SUB SUM (A,B)
S=A+B
PRINT “THE SUM OF TWO VALUES”;S
END SUB
Example 2. Program to find the sum of two values using SUB …… END SUB
REM PARAMETER PASSING BY VALUE
DECLARE SUB SUM (A,B)
CLS
CALL SUM (7,9)
END
SUB SUM (A,B)
S=A+B
PRINT “THE SUM OF TWO VALUES”;S
END SUB
Example 3. Program to find the average of two values
DECLARE SUB AVG ( X, Y )
CLS
INPUT “ ENTER THE FIRST VALUE”; X
INPUT “ENTER THE SECOND VALUE”;Y
CALL AVG (X,Y)
END
SUB AVG (X,Y)
AV= (X+Y)/2
PRINT “THE AVERAGE OF TWO VALUES”; AV
END SUB
i) Sample Programs using FUNCTION ….. END FUNCTION:
1. Program to find the circumference of a circle
DECLARE FUNCTION CIRC(R)
CLS
INPUT “ENTER THE RADIUS OF THE CIRCLE”; R
C = CIRC (R)
PRINT ” THE CIRCUMFERENCE OF THE CIRCLE”; C
END
FUNCTION CIRC(R)
X= 2*3.14*R
CIRC = X
END FUNCTION
2. Program to find the area of a rectangle
DECLARE FUNCTION RECT (L,B)
CLS
INPUT “LENGTH OF RECTANGLE”; L
INPUT BREADTH OF RECTANGLE”;B
R = RECT (L,B)
PRINT” THE AREA OF RECTANGLE”; R
END
FUNCTION AREA (L,B)
A = L*B
RECT = A
END FUNCTION
3. Program to find the volume of a cylinder
DECALRE FUNCTION VOL (R,H)
CLS
INPUT “INPUT RADIUS”;R
INPUT “INPUT HEIGHT”;H
V = VOL (R,H)
PRINT” THE VOLUME OF CYLINDER”; V
END
FUNCTION VOL (R,H)
X=3.14*R^2*H
VOL =X
END FUNCTION
ii) Sample Programs using SUB ….. END SUB:
1. Program to generate the 1,1,2,3,5 series upto 10th term.
DECLARE SUB XYZ ()
CLS
CALL XYZ
END
SUB XYZ
X=1
Y=1
FOR I= 1 TO 10
PRINT X
Z=X+Y
X=Y
Y=Z
NEXT
END SUB
2. Program to generate the following series 5,10,15,20 upto 10th term
DECLARE SUB SERIES ()
CLS
CALL SERIES
END
SUB SERIES
N=5
FOR I= 1 TO 10
PRINT N
N=N+5
END SUB
3. Program to generate the following series 2,22,222 upto 10th term
DECLARE SUB SERIES
CLS
CALL SERIES
END
SUB SERIES
N=2
FOR I=1 TO 10
PRINT N
N=N*10+2
END SUB
WAP to create a sub procedure to find the difference and a Function module to find the sum of two values.
REM EXAMPLE OF TWO MODULES
DECLARE SUB diff (a!, b!)
DECLARE FUNCTION sum (a, b)
DECLARE SUB dif (a, b)
CLS
INPUT "enter the first value"; a
INPUT "enter the second value"; b
PRINT "the sum"; sum(a, b)
CALL diff(a, b)
END
FUNCTION sum (a, b)
sum = a + b
END FUNCTION
SUB diff (a, b)
d = a - b
PRINT "the difference"; d
END SUB
WAP to check whether the input no is odd or even using function procedure.
DECLARE FUNCTION check (n)
CLS
INPUT "enter any number"; n
IF check(n) = 0 THEN
PRINT "even number "; n
ELSE
PRINT "odd number "; n
END IF
END
FUNCTION check (n)
r = n MOD 2
check = r
END FUNCTION
WAP to check whether the input no is odd or even using sub procedure.
DECLARE SUB check (n)
CLS
INPUT "enter any number"; n
CALL check(n)
END
SUB check (n)
n = n MOD 2
IF n = 0 THEN
PRINT "even number ";
ELSE
PRINT "odd number ";
END IF
END SUB
WAP to check whether the input no is multiple of 5 or not.
DECLARE SUB multi (n)
CLS
INPUT "enter a no"; n
CALL multi(n)
END
SUB multi (n)
IF n = n MOD 5 = 0 THEN
PRINT "it is multiple of 5"
ELSE
PRINT "not a multiple of 5"
END IF
END SUB
6.REM to check no divisible by 3 and 5
DECLARE SUB check (n)
CLS
INPUT "enter any number"; n
CALL check(n)
END
SUB check (n)
IF n MOD 3 = 0 AND n MOD 5 = 0 THEN
PRINT "numer divisible by 3 and 5"
ELSEIF n MOD 3 = 0 AND n MOD 5 <> 0 THEN
PRINT " divisible by 3 but not with 5"
ELSEIF n MOD 5 = 0 AND n MOD 3 <> 0 THEN
PRINT " divisible by 5 and not 3"
ELSE
PRINT "no is not divisible by 3 and 5"
END IF
END SUB
WAP to find the volume of a Cylinder using function module
DECLARE FUNCTION vol (r, h)
CLS
INPUT "enter radius of cylinder"; r
INPUT "enter height of cylinder"; h
v = vol(r, h)
PRINT "thr volume of cylinder"; v
END
FUNCTION vol (r, h)
vo = 3.141 * r * r * h
vol = vo
END FUNCTION
WAP to find the volume of a Cylinder using sub module
DECLARE SUB vol (r, h)
CLS
INPUT "enter radius of cylinder"; r
INPUT "enter height of cylinder"; h
CALL vol(r, h)
END
SUB vol (r, h)
vo = 3.141 * r * r * h
PRINT "the volume of cylinder"; vo
END SUB
WAP to enter radius and height of a cylinder
REM to print volume using sub procedure (hint: v=3.141*r*r*h)
REM to print total surface are using function (hint: t=2*3.141*r*(r*h)
DECLARE SUB VOLUME (R, H)
DECLARE FUNCTION TSA (R, H)
CLS
INPUT "enter radius"; R
INPUT "height"; H
CALL VOLUME(R, H)
PRINT "total surfae areA IS "; TSA(R, H)
END
FUNCTION TSA (R, H)
T = 2 * 314 * R * (R + H)
TSA = T
END FUNCTION
SUB VOLUME (R, H)
V = 3.141 * R * R * H
PRINT "THE VOLUME OF THE CYLINDER"; V
END SUB
Comments