QBASIC Programs SET-3 [SUB and FUNCTION]

Print This Page

 

                          

 

                           

  1. Write a program to display the reverse of input string .

Using Function Procedure.

 

DECLARE FUNCTION REV$(N$)

CLS

INPUT “ENTER A STRING” ; W$

PRINT  “REVERSED STRING::”;REV$(W$)

END

FUNCTION REV$(N$)

FOR I = LEN(N$) TO 1 STEP -1

D$=D$+MID$(N$,I,1)

NEXT I

REV$=D$

END FUNCTION

 

Using SUB Procedure

DECLARE SUB REV(N$)

CLS
INPUT “ENTER A WORD” ; W$

CALL REV(W$)

END

SUB REV(N$)
FOR I = LEN(N$) TO 1 STEP-1
C$=C$+MID$(N$,I,1)

NEXT I

PRINT “REVERSED STRING” ; C$

END SUB

  1. write a program to check whether the entered number is prime or composite.

using sub procedure

 

DECLARE SUB PRIME (N)

INPUT “ENTER ANY NUMBER”; N

CALL PRIME (N)

END

 

SUB PRIME (N)

C = 0

FOR I = 1 TO N

IF N MOD I = 0 THEN C = C + 1

NEXT I

IF C = 2 THEN

PRINT N; “IS PRIME NUMBER”

ELSE

PRINT N; “IS COMPOSITE NUMBER”

END IF

END SUB

 

Using function procedure

 

DECLARE FUNCTION PRIME(N)

CLS

INPUT “ENTER ANY NUMBER”; N

P = PRIME (N)

IF P = 2 THEN

PRINT N; “IS PRIME NUMBER”

ELSE

PRINT N; “IS COMPOSITE NUMBER”

END IF

END

 

FUNCTION PRIME (N)

C = 0

FOR I = 1 TO N

IF N MOD I = 0 THEN C = C + 1

NEXT I

PRIME = C

END FUNCTION

 

3.write a program to check input number is Armstrong or not?

USING SUB PROCEDURE

DECLARE SUB ARM (N)

CLS

INPUT “ENTER ANY NUMBER”; N

CALL ARM (N)

END

 

SUB ARM (N)

A = N

S = 0

WHILE N < > 0

R = N MOD 10

S = S  + R ^ 3

N = N \ 10

WEND

IF A = S THEN

PRINT A; “IS ARMSTRONG NUMBER”

ELSE

PRINT A; “IS NOT ARMSTRONG NUMBER”

END IF

END SUB

 

USING FUNCTION PROCEDURE

 

DECLARE FUNCTION ARM (N)

CLS

INPUT “ENTER ANY NUMBER”; N

A=N

AR = ARM (N)

IF A = AR THEN

PRINT A; “IS ARMSTRONG NUMBER”

ELSE

PRINT A; “IS NOT ARMSTRONG NUMBER”

END IF

END

 

FUNCTION ARM (N)

S = 0

WHILE N < > 0

R = N MOD 10

S = S + R ^ 3

N = N \ 10

WEND

ARM = S

END FUNCTION

 

4.write a program to find the area of square.

USING SUB PROCEDURE

 

DECLARE SUB AREA (L)

CLS

INPUT “ENTER LENGTH”; L

CALL AREA(L)

END

 

SUB AREA (L)

A = L ^ 2

PRINT “AREA OF SQUARE”; A

END SUB

 

USING FUNCTION PROCEDURE

 

DECLARE FUNCTION AREA (L)

CLS

INPUT “ENTER LENGTH”; L

AR = AREA(L)

PRINT “AREA OF SQUARE ”; AR

END

 

FUNCTION AREA (L)

A = L ^ 2

AREA = A

END FUNCTION

 

5.write a program to input a string and count the total numbers of vowels.

 

USING SUB PROCEDURE

DECLARE SUB COUNT (S$)

CLS

INPUT “ENTER ANY STRING”; S$

CALL COUNT(S$)

END

 

SUB COUNT (S$)

VC = 0

FOR I = 1 TO LEN(S$)

B$ = MID$(S$, I, 1)

C$ = UCASE$(B$)

IF C$ = “A” OR C$ = “E” OR C$ = “I” OR C$ = “O” OR C$ = “U” THEN

VC = VC + 1

END IF

NEXT I

PRINT “TOTAL NO. OF VOWELS= “; VC

END SUB

 

USING FUNCTION PROCEDURE

 

DECLARE FUNCTION COUNT (S$)

CLS

INPUT “ENTER ANY STRING”; S$

PRINT “TOTAL NO. OF VOWELS= “; COUNT(S$)

END

 

FUNCTION COUNT (S$)

VC = 0

FOR I = 1 TO LEN(S$)

B$ = MID$(S$, I, 1)

C$ = UCASE$(B$)

IF C$ = “A” OR C$ = “E” OR C$ = “I” OR C$ = “O” OR C$ = “U” THEN

VC = VC + 1

END IF

NEXT I

COUNT = VC

END FUNCTION

 

6.write a program to input a string and display only vowels.

 USING SUB PROCEDURE

DECLARE SUB DISPV (S$)

CLS

INPUT “ENTER ANY STRING”; S$

CALL DISPV(S$)

END

 

SUB DISPV(S$)

FOR I = 1 TO LEN(S$)

B$ = MID$(S$, I, 1)

C$ = UCASE$(B$)

IF C$ = “A” OR C$ = “E” OR C$ = “I” OR C$ = “O” OR C$ = “U” THEN

PRINT B$

END IF

NEXT I

END SUB

 

 

 

USING FUNCTION PROCEDURE

 

DECLARE FUNCTION DISPV (S$)

CLS

INPUT “ENTER ANY STRING”; S$

VC = DISPV(S$)

END

 

FUNCTION DISPV(S$)

FOR I = 1 TO LEN(S$)

B$ = MID$(S$, I, 1)

C$ = UCASE$(B$)

IF C$ = “A” OR C$ = “E” OR C$ = “I” OR C$ = “O” OR C$ = “U” THEN

PRINT B$

END IF

NEXT I

END FUNCTION

 

7.write a program to input string and display only consonants.

 

USING SUB PROCEDURE

 

DECLARE SUB DISPC (S$)

CLS

INPUT “ENTER ANY STRING”; S$

CALL DISPC(S$)

END

 

SUB DISPC(S$)

FOR I = 1 TO LEN(S$)

B$ = MID$(S$, I, 1)

C$ = UCASE$(B$)

IF C$ <> “A” AND C$ <> “E” AND C$ <> “I” AND C$ <> “O”

AND C$ <> “U” AND C$ <> ” ” AND C$ <> “.” THEN

PRINT B$

END IF

NEXT I

END SUB

 

USING FUNCTION PROCEDURE

 

DECLARE FUNCTION DISPC (S$)

CLS

INPUT “ENTER ANY STRING”; S$

CC = DISPC(S$)

END

 

FUNCTION DISPC(S$)

FOR I = 1 TO LEN(S$)

B$ = MID$(S$, I, 1)

C$ = UCASE$(B$)

IF C$ <> “A” AND C$ <> “E” AND C$ <> “I” AND C$ <> “O”

AND C$ <> “U” AND C$ <> ” ” AND C$ <> “.” THEN

PRINT B$

END IF

NEXT I

END FUNCTION

 

  1. write a program to check whether the input number is positive, negative or zero.

 

USING SUB PROCEDURE

 

DECLARE SUB CHECK (N)

CLS

INPUT “ENTER ANY NUMBER”; N

CALL CHECK (N)

END

SUB CHECK (N)

IF N > 0 THEN

PRINT N; IS POSITIVE NUMBER”

ELSEIF N < 0 THEN

PRINT N; IS NEGATIVE NUMBER”

ELSE

PRINT N; IS ZERO”

END IF

END SUB

 

USING FUNCTION PROCEDURE

 

DECLARE FUNCTION CHECK$ (N)

CLS

INPUT “ENTER ANY NUMBER”; N

PRINT N; “IS “; CHECK$(N)

END

 

FUNCTION CHECK$ (N)

IF N > 0 THEN

CHECK$ = “POSITIVE NUMBER”

ELSEIF N < 0 THEN

CHECK$ = “NEGATIVE NUMBER”

ELSE

CHECK$ = “ZERO”

END IF

END FUNCTION

 

9.write a program to print string in alternate capita .eg.CoMpUtEr

 

USING SUB PROCEDURE

 

DECLARE SUB ALTERNATE (A$)

INPUT “ENTER STRING”; A$

END

 

SUB ALTERNATE (A$)

FOR I= 1 TO LEN (A$)

B$= MID (A$, I, 1)

IF I MOD 2 = 0 THEN

B$= LCASE$ (B$)

ELSE

B$ = UCASE (B$)

END IF

ALT$ = ALT$ +B$

NEXT I “THE REQUIRED ALTERNATE IS” ;ALT$

END SUB

 

USING FUNCTION PROCEDURE

 

DECLARE FUNCTION ALTERNATE$ (A$)

CLS

INPUT “ENTER STING”; A$

PRINT “THE REQUIRED ALTERNATE IS”; ALTERNATE$ (A$)

END

 

FUNCTION ALTERNATE$ (A$)

FOR I = 1 TO LEM (A$)

B$ = MID$ (A$, I, 1)

IF I MOD 2 = 0 THEN

B$= LCASE$(B$)

ELSE

B$ = UCASE$ (B$)

END IF

ALT$ =ALT$ +B$

NEXT I

ALTERNATE$= ALT$

PRINT “THE REQUIRED ALTERNATE IS”; ALT$

END FUNCTION

 

 

10.write a program that prints the sum of even digits.

 

USING SUB PROCEDURE

 

DECLARE SUB SUMEVEN (N)

CLS

INPUT “ENTER ANY NUMBER”; N

CALL SUMEVEN (N)

END

 

SUB SUMEVEN (N)

S = 0

WHILE N < > 0

R = N MOD 10

IF R MOD 2 = 0 THEN S = S + R

N = N \ 10

WEND

PRINT “SUM OF EVEN DIGITS”; S

END SUB

 

USING FUNCTION PROCEDURE

 

DECLARE FUNCTION SUMEVEN (N)

CLS

INPUT “ENTER ANY NUMBER”; N

SU = SUMEVEN (N)

PRINT “SUM OF EVEN DIGITS”; SU

END

 

FUNCTION SUMEVEN (N)

S = 0

WHILE N < > 0

R = N MOD 10

IF R MOD 2 = 0 THEN S = S + R

N = N \ 10

WEND

SUMEVEN = S

END FUNCTION

 

11.write a program to convert the temperature given in the centigrade to farenhiet. (9C +160/5)

 

USING SUB PROCEDURE

 

DECLARE SUB CONVERT (C)

CLS

INPUT “ENTER TEMPERATURE IN CELCIUS”; C

CALL CONVERT (C)

END

 

SUB CONVERT (C)

F = C * (9 / 5) + 160

PRINT “TEMPERATURE IN FARENHEIT=”; F

END SUB

 

USING FUNCTION PROCEDURE

 

DECLARE FUNCTION CONVERT (C)

CLS

INPUT “ENTER TEMPERATURE IN CELSIUS”; C

PRINT “TEMPERATURE IN FAHRENHEIT=”; CONVERT (C)

END

 

FUNCTION CONVERT (C)

F = C * (9 / 5) + 160

CONVERT = F

END FUNCTION

 

 

12.write a program to find the perimeter of the rectangle.2(l+b)

 

USING SUB PROCEDURE

 

DECLARE SUB PERIMETER (L, B)

CLS

INPUT “ENTER LENGTH”; L

INPUT “ENTER BREADTH”; B

CALL PERIMETER(L, B)

END

 

SUB PERIMETER (L, B)

P = 2 * (L + B)

PRINT “PERIMETER OF RECTANGLE ”; P

END SUB

 

USING FUNCTION PROCEDURE

 

DECLARE FUNCTION PERIMETER (L, B)

CLS

INPUT “ENTER LENGTH”; L

INPUT “ENTER BREADTH”; B

PR = PERIMETER(L, B)

PRINT “PERIMETER OF RECTANGLE ”; PR

END

 

FUNCTION PERIMETER (L, B)

P = 2 * (L + B)

PERIMETER = P

END FUNCTION

 

Leave a Reply