QBASIC PROGRAMS FOR SEE [SUB AND FUNCTION PROCEDURE] (SET 4)

Print This Page
  1. Write  a program to check whether a supplied number is perfect square or not ?

Using SUB Procedure:

DECLARE SUB PRSQR(N)
CLS
INPUT “ENTER A NUMBER” ; N
CALL PRSQR(N)
END

SUB PRSQR(N)
S=SQR(N)
IF S = INT(S) THEN
PRINT “SUPPLIED NUMBER IS PERFECT SQUARE”
ELSE
PRINT “SUPPLIED NUMBER IS NOT PERFECT SQUARE”
ENDIF
END SUB

USING FUNCTION PROCEDURE 

DECLARE FUNCTION PRSQR$(N)
CLS
INPUT “ENTER A NUMBER” ; N
PRINT PRSQR$(N)
END

FUNCTION PRSQR$(N)
S=SQR(N)
IF S = INT(S) THEN
PRSQR$=”PERFECT SQUARE”
ELSE
PRSQR$=”NOT PERFECT SQUARE”
ENDIF
END SUB

2. Write a program to display the sum of individual digits of multi digit input number.

Using Sub Procedure

DECLARE SUB SUMDIG(N)
CLS
INPUT “ENTER MULTIGIT NUMBER” ; N
CALL SUMDIG(N)
END

SUB SUMDIG(N)
WHILE N<>0
R=N MOD 10
S=S+R
N=N\10
WEND
PRINT “SUM IS ” ; S
END SUB

Using Function Procedure:

DECLARE FUNCTION SDIG(N)
CLS
INPUT”ENTER A NUMBER”; N
PRINT “SUM IS ” ; SDIG(N)
END

FUNCTION SDIG(N)
WHILE N<>0
R=N MOD 10
S=S+R
N=N\10
WEND
SDIG=S
END FUNCTION

3. Write a program to count the total words present in an input string.

USING SUB PROCEDURE

DECLARE SUB WCOUNT(N$)
CLS
INPUT “ENTER A STRING” ; N$
CALL WCOUNT(N$)
END
SUB WCOUNT(N$)
C=1
FOR I = 1 TO LEN(N$)
C$=MID$(N$,I,1)
IF C$=” ” THEN C=C+1
NEXT I
PRINT “NUMBER OF C” ; C
END SUB

 

4. Write a program to print the longest string among three different supplied string.

Using SUB Procedure

DECLARE SUB LON(A$,B$,C$)
CLS
INPUT “ENTER FIRST STRING ” ; A$
INPUT “ENTER SECOND” ; S$
INPUT “ENTER THIRD” ; T$
CALL LON(A$,S$,T$)
END

SUB LON(A$,B$,C$)
A=LEN(A$)
B=LEN(B$)
C=LEN(C$)
IF A>B AND A>C THEN
LONG$=A$
ELSEIF B>A AND B>C THEN
LONG$=B$
ELSE
LONG$=C$
ENDIF
PRINT “LONGEST STRING” ; LONG$
END SUB

USING FUNCTION

DECLARE FUNCTION LONG$(A$,B$,C$)
CLS
INPUT “ENTER FIRST STRING ” ; A$
INPUT “ENTER SECOND” ; S$
INPUT “ENTER THIRD” ; T$
PRINT “LONGEST STRING” ;  LONG$(A$,S$,T$)
END

FUNCTION LONG$(A$,B$,C$)
A=LEN(A$)
B=LEN(B$)
C=LEN(C$)
IF A>B AND A>C THEN
LONG$=A$
ELSEIF B>A AND B>C THEN
LONG$=B$
ELSE
LONG$=C$
ENDIF
END FUNCTION

5. Write a program to find the factors of input number

DECLARE SUB FACT(N)
CLS
INPUT “ENTER A NUMBER”; N
CALL FACT(N)
END
SUB FACT(N)
FOR I = 1 TO N
IF N MOD I = 0 THEN PRINT I;
NEXT I
END SUB

6. Write a program to find the sum of factors of input number.

DECLARE SUB FACT(N)
CLS
INPUT “ENTER A NUMBER”; N
CALL FACT(N)
END
SUB FACT(N)
FOR I = 1 TO N
IF N MOD I = 0 THEN  S=S+I
NEXT I
PRINT “SUM OF FACTORS” ; S
END SUB

7. Check input number is prime or not.

CLS

DECLARE SUB PRIME(A)
CLS
INPUT “ENTER A NUMBER” ; N
CALL PRIME(N)
END
SUB PRIME(A)
FOR I = 1 TO A
IF A MOD I = 0 THEN C=C+1
NEXT I
IF C=2 THEN
PRINT “PRIME”
ELSE
PRINT “NOT PRIME
ENDIF
END SUB

8. Write a program to display Armstrong numbers from 1 to 500

DECLARE SUB ARM
CLS
CALL ARM
END
SUB ARM
FOR I = 1 TO 500
A=I
S=0
WHILE A<>0
R=N MOD 10
S=S+R^3
A=A\10
WEND
IF S=I THEN
PRINT “ARMSTRONG”
ELSE
PRINT “NOT ARMSTRONG”
ENDIF
END SUB

9. Display palindrome numbers from 1 to 1000

DECLARE SUB PAL()
CLS
CALL PAL
END
SUB PAL
FOR I = 1 TO 1000
A=I
S=0
WHILE A<>0
R=N MOD 10
S=S*10+R
A=A\10
WEND
IF S=I THEN
PRINT “PALINDROME”
ELSE
PRINT “NOT PALINDROME”
ENDIF
END SUB

10. Display prime numbers from 1 to 500

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

 

Leave a Reply