QBASIC PROGRAMS FOR SEE – SET 1 (FUNCTION AND SUB )

Print This Page
1. Write a program to calculate the volume of a cylinder using FUNCTION……END FUNCTION. [Hint:  PI*R^2*H]

Ans:

DECLARE FUNCTION VOL(R,H)

CLS

CONST PI=3.14

INPUT “Enter radius and height”; R, H

PRINT “The volume=”; VOL(R,H)

END

FUNCTION VOL(R,H)

V=PI*R^2*H

VOL=V

END FUNCTION

 

2. Write a program to test whether the given number is positive or negative using SUB……END SUB.

Ans:
DECLARE SUBTEST(N)

CLS

INPUT “Enter a number”; N

CALL TEST(N)

END

SUB TEST(N)

IF N>0 THEN

PRINT N; “is positive number”

ELSE

PRINT N; “is negative number”

END IF

END SUB

 

 

3.  Write a program to print the following series: 1,4,7,……. up to 10th term using FUNCTION……END FUNCTION.

ANS :

 DECLARE FUNCTION SERIES

CLS

D= SERIES

END

FUNCTION SERIES

FOR I = 1 TO 10

PRINT A;

A=A+3

NEXT I

END FUNCTION

 

4.  Write a program which accepts any three different numbers and find the maximum number among them using CALL statement.

 

Ans:

 DECLARE SUB MAX(A,B,C)

CLS

INPUT “Enter any three number’; A,B,C

CALL MAX(A,B,C)

END

SUB MAX(A,B,C)

IF A>B AND A>C THEN

PRINT A; “is maximum number”

ELSEIF B>A AND B>C THEN

PRINT B; “is maximum number”

ELSE

PRINT C; “is maximum number”

END IF

END SUB

 

5.  Write a program to display greater number among any two numbers using FUNCTION……. END FUNCTION.

ANS :

DECLARE FUNCTION GREAT(A,B)

CLS

INPUT “Enter any two number”; A,B

PRINT “The greater number is”; GREAT(A,B)

END

FUNCTION GREAT(A,B)

IF A>B THEN

GREAT= A

ELSE

GREAT=B

END IF

END FUNCTION

 

6. Write a program to print the following series: 1, 8, 27, 64,  …….. up to 10th terms using SUB procedure

 

Ans

DECLARE SUB SERIES()

CLS

CALL SERIES

END

SUB SERIES

FOR I =  1 TO 10

PRINT I^3;

NEXT I

END SUB

7. Write a program to find the factorial number of any non-negative number using FUNCTION…… END FUNCTION.

Ans:

 DECLARE FUNCTION FACT(N)

CLS

INPUT “Enter a non-negative number”; N

PRINT “The factorial of given number is”; FACT(N)

END

FUNCTION FACT(N)

F=1

FOR I =1 TO N

F=F*I

NEXT I

FACT=F

END FUNCTION

8. Write a program to display the series 55555, 5555, 555, 55, 5 using SUB………END SUB.

Ans:

DECLARE SUB SERIES()

CLS

CALL SERIES

END

SUB SERIES

A#= 55555

FOR I = 1TO 5

PRINT A#;

A#=A#\10

NEXT I

END SUB

 

9.  Write a program to display vowels and consonants of the entered string by using FUNCTION …….. END FUNCTION.

Ans:
DECLARE FUNCTION VOW(S$)

DECLARE FUNCTION CON(S$)

CLS

INPUT “Enter a word”; S$

VO= VOW(S$)

CO= CON(S$)

END

FUNCTION VOW(S$)

PRINT “The vowels are”

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$

NEXT I

END FUNCTION

FUNCTION CON(S$)

PRINT “The consonant are”

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” THEN PRINT B$

NEXT I

END FUNCTION

10.Write a program to display the reverse of the entered number by using SUB….. END SUB.

Ans:
DECLARE SUB REV(N)

CLS

INPUT “Enter a number”; N

CALL REV(N)

END

SUB REV(N)

S=0

WHILE N<>0

R=N MOD 10

S=S*10+R

N=N\10

WEND

PRINT “The reversed form=”;S

END SUB

 

11.   Write a program to declare a user defined function to return a given number in reverse order by using FUNCTION…… END FUNCTION.

Ans:

DECLARE FUNCTION REV(N)

CLS

INPUT “Enter a number”; N

PRINT “The reverse form is”; REV(N)

END

FUNCTION REV(N)

S=0

WHILE N<>0

R=N MOD 10

S=S*10+R

N=N\10

WEND

REV=S

END FUNCTION

 

12 Write a program to declare a SUB procedure module to generate multiplication table of any non-negative number, where number is passed as a parameter by using SUB….. END SUB statement.

Ans:
DECLARE SUB MUL(N)

CLS

INPUT “Enter a number”; N

CALL MUL(N)

END

SUB MUL(N)

FOR I = 1 TO 10

PRINT N; “*”;I; “=”; N*I

NEXT I

END SUB

13. Write a program to define a function procedure to display area of sphere where user has to input the required data in the main module. [Hint: Area of sphere: 4*PI*R^2]

Ans

DECLARE FUNTION AREA(R)

CLS

CONST PI= 3.14

INPUT “Enter radius”; R

PRINT “The area of sphere=”; AREA(R)

END

FUNCTION AREA(R)

A=4*PI*R^2

AREA=A

END FUNCTION

 

14.Write a program to define SUB procedure to display the following series of number: 1, 1, 2, 3, 5, …..up to 13thterm.

Ans:

 DECLARE SUB SERIES()

CLS

CALL SERIES

END

SUB SERIES

A=1

B=1

PRINT A;B;

FOR I =1 TO 11

C=A+B

PRINT C;

A=A+B

B=A+B

NEXT I

END SUB

15. Using user defined function, write a program to input monthly income in parameter then computer annual tax to be paid. The tax rate is 15% if annual income is above Rs. 200000, otherwise tax rate is 1%.

Ans

DECLARE FUNCTION TAX(I)

CLS

INPUT “Enter monthly income”; I

PRINT “Tax to be paid=”; TAX(I)

END

FUNCTION TAX(I)

A=I*12

IF A>200000 THEN

TAX=15/100*A

ELSE

TAX=1/100*A

END IF

END FUNCTION

 

16. Write a program to generate the series using SUB …… END SUB : -10, -8, -6, -4, ………. Up to 20th term.

Ans

DECLARE SUB SERIES()

CLS

CALL SERIES

END

SUB SERIES

A=-10

FOR I = 1 TO 20

PRINT A;

A=A+2

NEXT I

END SUB

17. Write a program using FUNCTION….END FUNCTION to calculate and print the total surface area of a sphere.

Ans

DECLARE FUNCTION TSA(R)

CLS

CONST PI=3.14

INPUT “Enter radius”; R

PRINT “The total surfacearea of sphere=”; TSA(R)

END

FUNCTION TSA(R)

T= 4*PI*R^2

TSA=T

END FUNCTION

 

18. Write a program to declare Sub procedure to print only the vowels from a given word.

Ans:

DECLARE SUB VOW(S$)

CLS

INPUT “Enter a word”; S$

CALL VOW(S$)

END

SUB VOW(S$)

PRINT “The vowels are”

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$

NEXT I

END SUB

19.  Write a program that asks any number and calculates its factors using a SUB procedure.

 

Ans

DECLARE SUB FACT(N)

CLS

INPUT “Enter a number”; N

CALL FACT(N)

END

SUB FACT(N)

PRINT “The factors of”; N; “are”

FOR I = 1 TO N

IF N MOD I = 0 THEN PRINT I

NEXT I

END SUB

 

 

20. Write a program that asks any three numbers and displays the difference between the greatest and the smallest value among the 3 supplied numbers using FUNCTION procedure.

Ans:

DECLARE FUNCTION DIF(A,B,C)

CLS

INPUT “Enter three numbers”; A, B, C

PRINT “THE DIFFERENCE=”; DIF(A, B, C)

END

FUNCTION DIF (A, B, C)

G = 0

S = 0

IF A > B AND A > B THEN

G = A

ELSEIF B > A AND B > C THEN

G = B

ELSE

G = C

END IF

IF A < B AND A < C THEN

S = A

ELSEIF B < A AND B < C THEN

S = B

ELSE

S = C

END IF

DIF = G – S

END FUNCTION

 

Leave a Reply