Modular Programming in Q BASIC Examples 2

Display the factorial of input number

SUB Procedure
DECLARE SUB FACT (N)
CLS
INPUT “Enter any number”; N
CALL FACT(N)
END
SUB FACT (N)
F = 1
FOR J = 1 TO N
F = F * J
NEXT J
PRINT “The factorial of input umber is”; F
END SUB


Print all prime numbers between 1 to 50.

SUB Procedure
DECLARE SUB PRIME ()
CLS
CALL PRIME
END
SUB PRIME
FOR P = 1 TO 50
C = 0
FOR J = 1 TO P
IF P MOD J = 0 THEN C = C + 1
NEXT J
IF C = 2 THEN
PRINT P;
END IF
NEXT P
END SUB


Display sum of numbers between 1 to 50 which are divisible by 5 and 7.

SUB Procedure
DECLARE SUB SUM ()
CLS
CALL SUM
END
SUB SUM
CLS
S = 0
FOR K = 1 TO 50
IF K MOD 5 AND K MOD 7 = 0 THEN
S = S + K
END IF
NEXT K
PRINT “Sum of numbers”; S
END SUB


Smallest number among three numbers

SUB procedure
DECLARE SUB SNUM ()
CLS
CALL SNUM
END
SUB SNUM
INPUT “Enter the first number”; A
INPUT “Enter the second number”; B
INPUT “Enter the third number”; C
IF A < B AND A < B THEN
PRINT “Smallest number”; A
ELSEIF B < A AND B < C THEN
PRINT “Smallest number”; B
END IF
END SUB


Perfect Square or Not

SUB Procedure
DECLARE SUB TEST (N)
CLS
INPUT “Enter any number”; N
CALL TEST(N)
END
SUB TEST (N)
CLS
R = SQR(N)
IF R = INT(R) THEN
PRINT N; “is a perfect square”
ELSE
PRINT N; “is not a perfect square”
END IF
END SUB


Input number is Prime or Not

SUB Procedure
DECLARE SUB PRIME (N)
INPUT “Enter any number”; N
CALL PRIME(N)
END
SUB PRIME (N)
C = 0
FOR J = 1 TO N
IF N MOD J = 0 THEN C = C + 1
NEXT J
IF C = 2 THEN
PRINT “It is prime number”
ELSE
PRINT “It is not prime number”
END IF
END SUB


HCF

DECLARE SUB HCLC ()
CLS
CALL HCLC
END
SUB HCLC
INPUT “Enter the first number”; A
INPUT “Enter the second number”; B
N = A
N = B
WHILE A <> 0
R = B MOD A
B = A
A = R
WEND
PRINT “HCF”; B
END SUB


Write a sub program to supply percentage from the keyboard and print grade. Use the following conditions to display the result.

Percentage              Grade
< 40                               Fail

>= 40 to < 60               C

>= 60 to < 80                B

> = 80 to <= 100          A

>100                             OUT of Range
DECLARE SUB GRADE (M)
CLS
INPUT “Enter the percentage”; M
CALL GRADE(M)
END
SUB GRADE (M)
SELECT CASE M
CASE IS < 40
R$ = “Fail”
CASE IS <= 60
R$ = “GRADE C”
CASE IS <= 80
R$ = “GRADE B”
CASE IS <= 100
R$ = “Out of Range”
END SELECT
PRINT “The result is”; R$
END SUB


Input string in alternate capital letters. If input string is ‘computer’ then output should be ‘CoMpUtEr’.

DECLARE SUB ALTCAP (A$)
INPUT “Enter the string”; A$
CALL ALTCAP(A$)
END
SUB ALTCAP (A$)
B$ = UCASE$(A$)
L = LEN(B$)
FOR K = 1 TO L
C$ = MID$(B$, K, 1)
IF K MOD 2 = 0 THEN
C$ = LCASE$(C$)
ELSE
C$ = UCASE$(C$)
END IF
D$ = D$ + C$
NEXT K
PRINT “Alternate case= “; D$
END SUB


Write a sub program to convert Binary number into its equivalent Decimal number.

DECLARE SUB DECIMAL (B$)
CLS
INPUT “Enter Binary no.”; B$
CALL DECIMAL(B$)
END
SUB DECIMAL (B$)
FOR I = LEN(B$) TO 1 STEP -1
D$ = MID$(B$, I, 1)
D = VAL(D$) * 2 ^ P
DEC = DEC + D
P = P + 1
NEXT I
PRINT “Decimal no.”; DEC
END SUB


Write a sub program to convert Decimal number into its equivalent Octal number.

DECLARE SUB OCTAL (D)
CLS
INPUT “Enter Decimal no.”; D
CALL OCTAL(D)
END
SUB OCTAL (D)
WHILE D <> 0
A = D MOD 8
D = FIX(D / 8)
B$ = STR$(A) + B$
WEND
PRINT “Octal number”; VAL(B$)
END SUB


Write a sub program to convert Decimal number into its equivalent Hexadecimal number.

DECLARE SUB HEXA (D)
CLS
INPUT “Enter Decimal no.”; D
CALL HEXA(D)
END
SUB HEXA (D)
WHILE D <> 0
A = D MOD 16
D = FIX(D / 16)
A$ = STR$(A)
IF A = 10 THEN A$ = “A”
IF A = 11 THEN A$ = “B”
IF A = 12 THEN A$ = “C”
IF A = 13 THEN A$ = “D”
IF A = 14 THEN A$ = “E”
IF A = 15 THEN A$ = “F”
B$ = B$ + A$
WEND
PRINT “Hexadecimal no.”; B$
END SUB


Write a sub program to convert Hexadecimal number into its equivalent decimal number.

DECLARE SUB DECI (H$)
CLS
INPUT “Enter Hexadecimal no.”; H$
CALL DECI(H$)
END
SUB DECI (H$)
FOR I = LEN(H$) TO 1 STEP -1
A$ = MID$(H$, I, 1)
IF A$ = “A” THEN A$ = “10”
IF A$ = “B” THEN A$ = “11”
IF A$ = “C” THEN A$ = “12”
IF A$ = “D” THEN A$ = “13”
IF A$ = “E” THEN A$ = “14”
IF A$ = “F” THEN A$ = “15”
A = VAL(A$) * 16 ^ P
DEC = DEC + A
P = P + 1
NEXT I
PRINT “Decimal no.”; DEC
END SUB


Write a sub program to convert Octal number into its equivalent decimal number.

DECLARE SUB DECI (D)
CLS
INPUT “Type any Octal No.”; N
CALL DECI(N)
END
SUB DECI (X)
WHILE X <> 0
R = X MOD 10
S = S + R * 8 ^ P
P = P + 1
X = X \ 10
WEND
PRINT “Decimal Equivalent”; S
END SUB


Write a sub program to input any five numbers into an array and find out the sum of all the numbers.

DECLARE SUB SUM (N ())
CLS
DIM N(5)
FOR J = 1 TO 5
INPUT “Enter the number”; N(J)
NEXT
CALL SUM(N())
END
SUB SUM (N())
SU = 0
FOR I = 1 TO 5
SU = SU + N(I)
NEXT
PRINT “Sum of numbers”; SU
END SUB


Write a sub program to input five numbers and sort them in the ascending order.

DECLARE SUB SORT (N ())
DIM N(5)
FOR I = 1 TO 5
INPUT “Enter the numbers”; N(I)
NEXT I
CALL SORT(N())
FOR J = 1 TO 5
PRINT N(J);
NEXT J
END
SUB SORT (N())
FOR P = 5 TO 1 STEP -1
FOR Q = 1 TO P – 1
IF N(Q) > N(Q + 1) THEN SWAP N(Q), N(Q + 1)
NEXT
NEXT
END SUB


Write a sub program to draw a circle on the screen.

DECLARE SUB DRAWCIRCLE ()
CLS
CALL DRAWCIRCLE
END
SUB DRAWCIRCLE
SCREEN 2
CIRCLE (210, 50), 100, 1
END SUB

 

Modular Programming 1 (Click Here)