QBASIC SOLVED:Set4














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
 11.To display given pattern:
 CLS
FOR i = 30 TO 22 STEP -2
    FOR j = 30 TO i STEP -2
        PRINT j;
    NEXT j
    PRINT
NEXT i
END

output:
30
30 28
30 28 26
30 28 26 24
30 28 26 24 22

12.To display the given pattern:as flag of NEPAL.
*
* *
* * *
* * * *
*
* *
* * * *
*
*
*
*

ans:
CLS
S$="****"
FOR i=1 TO LEN(S$)
PRINT LEFT$(S$,I)
NEXT I
FOR J=1 TO LEN(S$)
PRINT LEFT$(S$,J)
NEXT J
FOR k=1 TO LEN(S$)
PRINT MID$(S$,k,1)
NEXT k
END



Qbasic Program: using conditions


13. WAP to input number of passengers and their destination. The program should calculate the total bus fare and the discount amount according to the following conditions.
Rates for the different destinations:
Destinations                             Rate
Pokhara                                    450 per person
Butwal                                     500 per person
Chitwan                                   300 per person
Discount Rate
If the number of passenger is 5 or above then discount=5% in the total amount.

CLS
INPUT "ENTER NO. OF PASSENGER"; P
INPUT "ENTER DESTINATIONS"; D$
IF UCASE$(D$) = "POKHARA" THEN
T = P * 450
ELSEIF UCASE$(D$) = "BUTWAL" THEN
T = P * 500
ELSEIF UCASE$(D$) = "CHITWAN" THEN
T = P * 300
END IF
IF P >= 5 THEN D = 5 / 100 * T
F = T - D
PRINT "TOTAL BUS FARE="; F
END

USING SUB PROCEDURE

DECLARE SUB TOTAL(P, D$)
CLS
INPUT "ENTER NO. OF PASSENGER"; P
INPUT "ENTER DESTINATIONS"; D$
CALL TOTAL (P, D$)
END

SUB TOTAL (P, D$)
IF UCASE$(D$) = "POKHARA" THEN
T = P * 450
ELSEIF UCASE$(D$) = "BUTWAL" THEN
T = P * 500
ELSEIF UCASE$(D$) = "CHITWAN" THEN
T = P * 300
END IF
IF P >= 5 THEN D = 5 / 100 * T
F = T - D
PRINT "TOTAL BUS FARE="; F
END SUB

14. WAP to input total number of words and compute telegram charges which are as follows:
For the first 20 words    :           Rs 15
For the next 20 words : Rs 10
Above that for each word:         Re 1

CLS
INPUT "ENTER HOW MANY WORDS"; W
IF W <= 20 THEN
C = 15
ELSEIF W > 20 AND W <= 40 THEN
C = 15 + 10
ELSEIF W > 40 THEN
C = 15 + 10 + (W - 40)
END IF
PRINT "NUMBER OF WORDS="; W
PRINT "TOTAL CHARGE="; C

END

USING SUB PROCEDURE

DECLARE SUB TOTAL(W)
CLS
INPUT "ENTER HOW MANY WORDS"; W
CALL TOTAL(W)
END
SUB TOTAL(W)
IF W <= 20 THEN
C = 15
ELSEIF W > 20 AND W <= 40 THEN
C = 15 + 10
ELSEIF W > 40 THEN
C = 15 + 10 + (W - 40)
END IF
PRINT "NUMBER OF WORDS="; W
PRNT "TOTAL CHARGE="; C
END SUB

15. In order to discourage heavy consumption of electricity, the electricity board charges the following rates:
First 80 units:   Rs 4 per unit
Next 60 units: Rs 6 per unit
Above that :     Rs 7 per unit
Every user has to pay a minimum charge of Rs. 100 irrespective of the total amount of electricity consumed. In case total consumption is more than 300 units an additional charge of 10% is added. WAP to read consumer’s name, units consumed and print out total amount to pay along with name of consumer.

CLS
INPUT "ENTER NAME OF A CUSTOMER"; N$
INPUT "ENTER UNIT CONSUMED"; U
IF U <= 80 THEN
C = U * 4
ELSEIF U >= 81 AND U <= 140 THEN
C = 80 * 4
C1 = (U - 80) * 6
ELSE
C = 80 * 4
C1 = 60 * 6
C2 = (U - 140) * 7
END IF
T = 100 + C + C1 + C2
PRINT "NAME OF CUSTOMER"; N$
PRINT "TOTAL UNIT CONSUMPTION"; U
PRINT "TOTAL AMOUNT"; T
END


USING SUB PROCEDURE

DECLARE SUB TOTAL(N$, U)
CLS
INPUT "ENTER NAME OF A CUSTOMER"; N$
INPUT "ENTER UNIT CONSUMED"; U
CALL TOTAL (N$, U)
END

SUB TOTAL (N$, U)
IF U <= 80 THEN
C = U * 4
ELSEIF U >= 81 AND U <= 140 THEN
C = 80 * 4
C1 = (U - 80) * 6
ELSE
C = 80 * 4
C1 = 60 * 6
C2 = (U - 140) * 7
END IF
T = 100 + C + C1 + C2
PRINT "NAME OF CUSTOMER"; N$
PRINT "TOTAL UNIT CONSUMPTION"; U
PRINT "TOTAL AMOUNT"; T
END SUB

16. WAP to supply percentage from the keyboard and print division using sub – program. Use the following conditions:
Percentage                                Division
Per <40                                     Fail
Per>=40 and P<50                     Third
Per>=50 and P<60                     Second
Per>=60 and P<80                     First
Per>=80 and P<100                   Distinction

CLS
INPUT “ENTER PERCENTAGE”; P
IF P>=80 AND P<=100 THEN
PRINT “DIVISION”
ELSEIF P>=60 AND P<80 THEN
PRINT “FIRST DIVISION”
ELSEIF P>=50 AND P<60 THEN
PRINT “SECOND DIVISION”
ELSEIF P>=40 AND P<50 THEN
PRINT “THIRD DIVISION”
ELSEIF P>=0 AND P<40 THEN
PRINT “FAIL”
ELSE
PRINT “PLEASE ENTER THE NUMBER BETWEEN 0 TO 100”
END IF


Comments

Popular

Multimedia and its applications

File handling on QBASIC:SOLVED

Networking and Telecommunication