Sunday 9 December 2018

Q Basic Programming


Library Functions of QBasic Examples
Convert Decimal to Binary
CLS

INPUT “ENTER DECIMAL NUMBER”; D

WHILE D < > 0

R = D MOD 2

S$ = STR$(R) + S$

D = D \ 2

WEND

PRINT “BINARY EQUIVALENT VALUE=”; S$

END



Convert Binary to Decimal

CLS

INPUT “ENTER BINARY NUMBER”; N$

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

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

S = S + VAL(B$) * 2 ^ P

P = P + 1

NEXT I

PRINT “DECIMAL EQUIVALENT VALUE=”; S

END



Convert Decimal to Hexadecimal

CLS

INPUT “ENTER DECIMAL NUMBER”; D

WHILE D <> 0

R = D MOD 16

IF R < 10 THEN

S$ = STR$(R) + S$

ELSE

S$ = CHR$(R + 55) + S$

END IF

D = D \ 16

WEND

PRINT “HEXADECIMAL EQUIVALENT VALUE=”; S$

END



Convert Hexadecimal to decimal

CLS

INPUT “ENTER HEXADECIMAL NUMBER”; N$

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

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

IF B$ = “A” THEN B$ = “10”

IF B$ = “B” THEN B$ = “11”

IF B$ = “C” THEN B$ = “12”

IF B$ = “D” THEN B$ = “13”

IF B$ = “E” THEN B$ = “14”

IF B$ = “F” THEN B$ = “15”

S = S + VAL(B$) * 16 ^ P

P = P + 1

NEXT I

PRINT “DECIMAL EQUIVALENT VALUE=”; S

END



Input any string and count total no of vowel

CLS

INPUT “ENTER ANY STRING”; 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





Given word is Palindrome or Not

CLS
INPUT “Enter any Word”; A$
FOR i = LEN(A$) TO 1 STEP -1
B$ = B$ + MID$(A$, i, 1)
NEXT i
IF B$ = A$ THEN
PRINT “It is palindrome”
ELSE
PRINT “it is not palindrome”
END IF
END



Given Number is perfect Square or Not

CLS
INPUT “enter any number”; n
A = SQR(n)
IF A = INT(A) THEN
PRINT “perfect square”
ELSE
PRINT “Not perfect square”
END IF
END



Digital Clock

CLS
PRINT “***************************************************************************************************”
LOCATE 2, 30: PRINT “Digital Clock”
PRINT “***************************************************************************************************”
FOR i = 1 TO 10
LOCATE 11, i + 32: PRINT “-”
LOCATE 13, i + 32: PRINT “-”
NEXT i
LOCATE 12, 33: PRINT “~”
LOCATE 12, 42: PRINT “~”
a$ = TIME$
DO
LOCATE 12, 34: PRINT a$
a$ = TIME$
LOOP WHILE INKEY$ = “”
PRINT “***************************************************************************************************”
LOCATE 2, 30: PRINT “Digital Clock”
PRINT “***************************************************************************************************”

END



Armstrong or Not Armstrong

CLS
INPUT “enter any number”; num
n = num

WHILE num <> 0
r = num MOD 10
num = num \ 10
sum = sum + (r ^ 3)
WEND
IF n = sum THEN
PRINT “armstrong”
ELSE
PRINT “not arm strong”
END IF
END



Input numbers and find sum

CLS
INPUT “enter any number”; n
WHILE n <> 0
r = n MOD 10
s = s + r
n = INT(n / 10)
WEND
PRINT “sum of digits”; s
END





Display Pattern

N
NE
NEP
NEPA
NEPAL

CLS

A$=”NEPAL”

FOR I = 1 TO LEN (A$)

PRINT LEFT$(A$,i)

NEXT i

END




Display

NEPAL
NEPA
NEP
NE
N

CLS

A$ = “NEPAL”

FOR i = LEN(A$) TO 1 STEP -1

PRINT RIGHT$(A$, i)

NEXT i

END

Display:

PROGRAMMING
PROGRAMMIN
PROGRAMMI
PROGRAMM
PROGRAM
PROGRA
PROGR
PROG
PRO
PR
P

CLS
s$ = “PROGRAMMING”
t = 1
r = 1
FOR i = LEN(s$) TO 1 STEP -1
PRINT TAB(t); MID$(s$, r, i)
NEXT i
END

Display: 

PROGRAMMING
ROGRAMMING
OGRAMMING
GRAMMING
RAMMING
AMMING
MMING
MING
ING
IN
G

CLS
s$ = “PROGRAMMING”
t = 1
r = 1
FOR i = LEN(s$) TO 1 STEP -1
PRINT TAB(t); MID$(s$, r, i)

r = r + 1
NEXT i
END






Display

L
AL
PAL
EPAL
NEPAL

CLS

A$ = “NEPAL”

FOR I = 1 TO LEN(A$)

PRINT RIGHT$(A$, I)

NEXT I

END








Display

NE

EP

PA

AL

CLS
a$ = “NEPAL”
x = LEN(a$)
FOR i = 1 TO x – 1
PRINT TAB(i); MID$(a$, i, 2)
NEXT i
END



Display

P

EPA

NEPAL

CLS
s$ = “NEPAL”
C = 1: b = 35
FOR i = 3 TO 1 STEP -1
PRINT TAB(b); MID$(s$, i, C)
C = C + 2
b = b – 1
NEXT i
END




Display

L

    A

        P

              E

                  N

CLS
s$ = “NEPAL”
t = 3
FOR i = LEN(s$) TO 1 STEP -1

PRINT TAB(t); MID$(s$, i, 1)
t = t + 1
NEXT i
END




Display

                                                        L

                                              A

                                     P

                            E

                  N

CLS
s$ = “NEPAL”
t = 8
FOR i = LEN(s$) TO 1 STEP -1

PRINT TAB(t); MID$(s$, i, 1)
t = t – 1
NEXT i
END




Display

                                                         N

                                             E

                                     P

                            A

                  L

CLS
s$ = “NEPAL”
t = 8
FOR i = 1 TO LEN(s$)

PRINT TAB(t); MID$(s$, i, 1)
t = t – 1
NEXT i
END




Display

N

     E

          P

               A

                        L



CLS
s$ = “NEPAL”
t = 8
FOR i = 1 TO LEN(s$)

PRINT TAB(t); MID$(s$, i, 1)
t = t + 1
NEXT i
END




Display

N

E

P

A

L

CLS
s$ = “NEPAL”

FOR i = 1 TO LEN(s$)

PRINT MID$(s$, i, 1)

NEXT i
END




Display

L

A

P

E

N

CLS
s$ = “NEPAL”

FOR i = LEN(s$) TO 1 STEP -1

PRINT MID$(s$, i, 1)

NEXT i
END




Display:

*
**
***
****
*****
*
**
***
****
*****
*
*
*
*
*

CLS
A$ = “*********”
A = 1
B = 1
FOR I = 1 TO 5
COLOR 4
PRINT MID$(A$, A, B)
B = B + 1
NEXT I
A = 1
B = 1
FOR J = 1 TO 5
COLOR 6
PRINT MID$(A$, A, B)
B = B + 1
NEXT J
FOR K = 1 TO 5
COLOR 5
PRINT MID$(A$, K, 1)
NEXT K
END




Display

*
**
***
****
*****

CLS

A$=”****”

FOR I = 1 TO LEN (A$)

PRINT LEFT$(A$,i)

NEXT i

END

OR

CLS
A$ = “*********”
A = 1
B = 1
FOR I = 1 TO 5
PRINT MID$(A$, A, B)
B = B + 1
NEXT I
END

Display

*****
****
***
**
*

CLS

A$ = “*****”

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

PRINT LEFT$(A$, I)

NEXT I

END

OR

CLS
A$ = “*********”
A = 1
B = 5
FOR I = 5 to 1 step -1
PRINT MID$(A$, A, B)
B = B -1
NEXT I
END

R

GRA

OGRAM

ROGRAM

PROGRAM

CLS
s$ = “PROGRAM”
c = 1: b = 40
FOR i = 5 TO 1 STEP -1
PRINT TAB(b); MID$(s$, i, c)
c = c + 2
b = b – 1
NEXT i
END



*********

*******

*****

***

*

CLS
A$ = “***********”
C = 9: B = 30
FOR I = 1 TO 5
PRINT TAB(B); MID$(A$, I, C)
C = C – 2
B = B + 1
NEXT I
END




Display:

A
RAM
GRAMM
OGRAMMI
ROGRAMMIN
PROGRAMMING

CLS
s$ = “PROGRAMMING”
t = 6
FOR i = 1 TO LEN(s$) STEP 2
PRINT TAB(t); MID$(s$, t, i)
t = t – 1
NEXT i
END

Display:

PROGRAMMING
ROGRAMMIN
OGRAMMI
GRAMM
RAM
A

CLS
s$ = “PROGRAMMING”
t = 1
FOR i = LEN(s$) TO 1 STEP -2
PRINT TAB(t); MID$(s$, t, i)
t = t + 1
NEXT i
END



N

E         E       E

P              P            P

A                     A                 A

L                                 L                             L





CLS
s$ = “NEPAL”
t = 12: Q = 10: R = 14: s = 12
PRINT TAB(t); “N”
FOR K = 2 TO 5
z$ = MID$(s$, K, 1)
PRINT TAB(Q); z$; TAB(R); z$; TAB(s); z$
Q = Q – 2: R = R + 2: s = s + 0
NEXT
END





Examples of SUB PROCEDURE
 
WRITE A PROGRAM TO

Quick Links













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





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







Print the following series: 1,4,7,……. up to 10th term .

Ans:

DECLARE FUNCTION SERIES

CLS

D= SERIES

END

FUNCTION SERIES

FOR I = 1 TO 10

PRINT A;

A=A+3

NEXT I

END FUNCTION





 Accept any three different numbers and find the maximum number among them.

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







Display greater number among any two numbers using FUNCTION……. END FUNCTION.

Ans:

DECLARE FUNCTION MAX(A,B)

CLS

INPUT “Enter any two number”; A,B

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

END

FUNCTION MAX(A,B)

IF A>B THEN

MAX= A

ELSE

MAX=B

END IF

END FUNCTION



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





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







Display the series 55555, 5555, 555, 55, 5.

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





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







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





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





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





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










No comments:

Post a Comment

MCQ Question Bank Microsoft PowerPoint