Latest Post:
Loading...

Sub Procedure and Programs solution using Sub procedure

SUB PROCEDURE 

Ø Declaring the sub procedure
Ø Define the sub procedure
Ø Call the sub procedure
Ø Declaring a sub procedure






DECLARE Statement: DECLARE statement is used to declare the sub procedure
                        SYNTAX: DECLARE SUB <name>(parameter lists) •
Ø Eg: DECLARE SUB sum(A, B)
Ø DECLARE SUB AOR(L, B)
Ø       name is the name of the sub-program (sub-procedure or procedure) that will be used to call the procedure
Ø       parameter list indicates the number and type of arguments that will be used to call the procedure
Defining a sub procedure
A sub procedure is defined by using SUB…END SUB
            Syntax:
            SUB <name> (parameter List)
                        block of code …..……..
            END SUB
 
SUB sum(A, B)
             S = A + B
            PRINT “sum of two number=“; S
END SUB
 Calling a sub procedure
 CALL statement: CALL statement is used to call/invoke a sub procedure
Syntax:
CALL <name> (<argument list>)
Here, name is the name of the SUB
And argument list àlists the variables
 Examples: CALL SUM(A, B)
 or constants passed to the SUB
 WAP to find sum of any two number.
 
Main module
DECLARE SUB sum(A, B) CLS
INPUT “Enter any two number:”; A, B
 CALL sum(A, B)
END
SUB module
SUB sum(A, B)
S= A + B        
PRINT “sum of two number=“; S
END SUB
 WAP to find area of 4 wall. 2H(l+b)
  DECLARE SUB AOF( L, B , H)
 CLS
INPUT “Enter Length:”; L
INPUT “Enter Breadth” ; B
END
 SUB AOF( L, B , H)
 A = 2*H ( L + B )
PRINT “Area of four wall=“;A
END SUB
 
DECLARE SUB VOC(pi, r, h) CLS
INPUT “Enter radius:”; r
INPUT “Enter height:”; h
 CALL VOC(pi, r, h)
END
 
SUB VOC( pi,r, h)
Vol = pi * r ^2 * h
PRINT “Volume of Cylinder=“; Vol
END SUB
 Conditional statements (control flow statement)
• It is used to control the flow of execution of programs
Ø if….then
Ø If…then…else
Ø Expanded if…
Ø Select case
 Write a program to display the input number is odd or even.
DECLARE SUB CHECK(N)
CLS
INPUT “ENTER A NATURAL NUMBER:” ; N CALL CHECK(N)
END
 
 
SUB CHECK(N)
IF N MOD 2 = 0 THEN
PRINT N; “IS EVEN NUMBER” ELSE
PRINT N; “IS ODD NUMBER” END IF
END SUB
 
PRIME AND COMPOSITE
DECLARE SUB CHECK(N) CLS
INPUT “ENTER ANY NATURAL NUMBER EXCEPT 1” ; N
CALL CHECK(N)
END
 SUB CHECK (N)
FOR I = 1 TO N
IF N MOD I = 0 THEN C = C + 1
NEXT I
IF C =2 THEN
PRINT N; “IS PRIME”
ELSE
PRINT N; “IS COMPOSITE” END IF
END SUB
 LOOPING
Looping is the repetition of program or statements until or unless given condition is satisfied.
LOOPING:à 3 TYPES
Ø FOR LOOP
Ø WHILE LOOP
Ø DO WHILE LOOP
 FOR LOOP
SYNTAX: FOR (COUNTER VARIABLE) = START TO END STEP           [increment, decrement]
……………… LINE OF CODE…
NEXT COUNTER VARIABLE
Examples
 CLS
FOR A = 1 TO 10 STEP 1 PRINT A;
NEXT A
END
OUTPUT = 1 2 3 4 5 6 7 8 9 10
 Questions 1,2,3………UPTO 15TH TERM
DECLARE SUB SERIES()
             CLS
            CALL SERIES
            END
SUB SERIES
             FOR I = 1 TO 15
            PRINT I;
            NEXT I
 END SUB
 
Questions 1, 4, 7……UPTO 10TH TERM          Logic: INCREMENT BY 3
 
DECLARE SUB SERIES(a)
CLS
a = 1
CALL SERIES(a)
 END
SUB SERIES
 FOR I = 1 TO 10 PRINT a;
a = a + 3
NEXT
END SUB
 WAP a program to solve following series using sub procedure.
a) 1, 7, 13………up to 10th terms
 b) 2, 4 ,6, 8 ……up to 20th terms
c) 1, 4, 9, 16 ……………up to 12th terms
d) 1, 8, 27, ………….up to 14th terms
e) 1, 1, 2, 3, 5,………………upto 10th terms
 
1, 4, 9, 16 ……………up to 12th terms
DECLARE SUB SERIES()
CLS
CALL SERIES
 END
SUB SERIES
FOR I = 1 TO 12
PRINT I^2;
 NEXT I
END SUB
 
1, 1, 2, 3, 5,………………upto 10th terms
DECLARE SUB FIBO()
CLS
CALL FIBO
END
SUB FIBO
A = 1
B = 1
For I = 1 to 10
PRINT A;
C= A + B
A= B
B=C
NEXT I
END SUB
 
22, 11, 34, 17, 52 ………….UPTO 10TH TERMS
 
 
DECLARE SUB SERIES()
CLS
CALL SERIES
END
 SUB SERIES
A= 22
FOR I = 1 TO 10
 PRINT A;
IF A MOD 2 = 0 THEN
A=A/2
ELSE
A= A*3+1
END IF
NEXT I
END SUB
 
HOME ASSIGNMENT
·        WAP to ask a number and display in reverse order using sub procedure.
·        WAP to ask a number and check whether the number is palindrome or not.
·        WAP to ask a number and check whether the number is Armstrong or not.
·        WAP to ask a number using sub procedure.
While loop:
Syntax:
while<condition>
…..line of code
wend
 Write a program to ask a number and display in reverse order
DECLARE SUB REV(N)
 CLS
INPUT “ENTER A NUMBER:”;N
CALL REV(N)
END
SUB REV(N)
WHILE N < > 0
 R = N MOD 10
RE = RE*10 + R
N = INT(N/10)
WEND
PRINT “REVERSE NUMBER=” ;RE
END SUB
 
WAP to ask a number and check whether the number is palindrome or not.
DECLARE SUB REV(N)
 CLS
INPUT “ENTER A NUMBER:”;N
CALL REV(N)
END
 SUB REV(N)
 LET M = N
WHILE N < > 0
R = N MOD 10
RE = RE*10 + R
N = INT(N/10)
WEND
IF M = RE THEN
PRINT N; “IS PALINDROME”
ELSE
PRINT N; “IS NOT PALINDROME”
END IF
END SUB
 WAP to ask a number and display SUM OF INDIVIDUAL DIGIT IF 532 then 5+3+2 =10
DECLARE SUB SOD(N)
 CLS
INPUT “ENTER A NUMBER:”;N
CALL SOD(N)
END
SUB SOD(N)
WHILE N < > 0
R = N MOD 10
SUM = SUM + R
 N = INT(N/10)
WEND
PRINT “SUM OF INDIVIDUAL DIGIT=” ;SUM
END SUB
 WAP to ask a number and display SUM OF ODD DIGIT IF 532 then 5+3 =10
DECLARE SUB SOD(N)
 CLS
INPUT “ENTER A NUMBER:”;N
CALL SOD(N)
END
SUB SOD(N)
 WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 1 THEN
 SUM = SUM + R
N = INT(N/10)
WEND
PRINT “SUM OF ODD DIGIT=” ;SUM
 END SUB
 WAP to ask a number and display SUM OF even DIGIT IF 8532à                     8+2 =10
 
DECLARE SUB SED(N) CLS
INPUT “ENTER A NUMBER:”;N
CALL SED(N)
END
SUB SED(N)
WHILE N < > 0
R = N MOD 10
IF R MOD 2 = 0 THEN SUM = SUM + R
N = INT(N/10)
WEND
PRINT “SUM OF EVEN DIGIT=” ;SUM
END SUB

 

 

 

 

 

 

Post a Comment

Khem Raj Poudel
Khem Raj Poudel
Khem Raj Poudel