Sunday, June 22, 2014

QBASIC SOLUTIONS

Set-1
1.               Write a program to ask principal amount, time and rate and calculate simple interest using Sub procedure. (I=PTR/100)
=>
DECLARE SUB CALC(P,T,R)
REM TO CALCULATE SIMPLE INTEREST
CLS
INPUT ”Enter principal, time and rate”; P,T,R
CALL CALC(P,T,R)
END

SUB CALC(P,T,R)
I=(P*T*R)/100
PRINT “The simple interest=”;I
END SUB


2.             Write a program to ask any two numbers and find out their sum, product and difference using SUB procedure.
=>
DECLARE SUB SUM(A,B)
DECLARE SUB PRODUCT(A,B)
DECLARE SUB DIFFERENCE(A,B)
REM TO FIND SUM, PRODUCT AND DIFFERENCE OF TWO GIVEN NUMBERS
CLS
INPUT “Enter any two number”; A,B
CALL SUM(A,B)
CALL PRODUCT(A,B)
CALL DIFFERENCE(A,B)
END

SUB SUM(A,B)
REM SUM
S=A+B
PRINT “The sum of”;A;”and”;B;”is”;S
END SUB

SUB PRODUCT(A,B)
REM PRODUCT
P=A*B
PRINT “The product of”;A;”and”;B;”is”;P
END SUB

SUB DIFFERENCE(A,B)
REM DIFFERENCE
D=A-B
PRINT “The difference of”;A;”and”;B;”is”;D
END SUB


3.              Write a program to ask any two numbers and find out the greater number using SUB procedure.
=>
DECLARE SUB GREAT(A,B)
REM TO FIND OUT GREATER NUMBER AMONG TWO GIVEN NUMEBRS
CLS
INPUT “Enter any two numbers”;A,B
CALL GREAT(A,B)
END

SUB GREAT(A,B)
IF A>B THEN
PRINT A;”is greater”
ELSE
PRINT B;”is greater”
END IF
END SUB


4.             Write a program to ask a number and find the sum of digits using SUB procedure. If number is 123 then sum is 6.
=>
DECLARE SUB SUM(N)
REM TO FIND THE SUM OF GIVEN DIGITS
CLS
INPUT ”Enter a number”;N
CALL SUM(N)
END

SUB SUM(N)
S=0
WHILE N<>0
R=N MOD 10
S=S+R
N=N\10
WEND
PRINT “The sum of digits of given number is”;S
END SUB


5.             Write a program to find the sum of first ten natural numbers using SUB procedure.
=>
DECLARE SUB SUM()
REM TO FIND THE SUM F FIRST TEN NATUAL NUMEBRS
CLS
CALL SUM
END

SUB SUM
N=10
S=N*(N+1)/2
PRINT “THE SUM OF FIRST TEN NATURAL NUMBERS=”;S
END SUB


6.             Write a program to ask a number and display its square value using SUB procedure.
=>
DECLARE SUB SSQR(N)
REM TO DISPLAY SQUARE VALUE OF GIVEN NUMBER
CLS
INPUT “Enter any number”;N
CALL SSQR(N)
END

SUB SSQR(N)
SQ=N^2
PRINT  “The square of given number is”;SQ
END SUB


7.              Write a program to ask a number and display its multiplication table using SUB procedure.
=>
DECLARE SUB TABLE(N)
REM TO DISPLAY  THE MULTIPLICATION TABLE OF GIVEN NUMBER
CLS
INPUT “Enter any number”;N
CALL TABLE(N)
END

SUB TABLE(N)
FOR I = 1 TO 10
PRINT N;”*”;I;”=”;N*I
NEXT I
END SUB


8.             Write a program to ask your name and print 10 times using SUB procedure.
=>
DECLARE SUB NAME(N$)
REM TO DISPLAY YOUR NAME TEN TIMES
CLS
INPUT “Enter your name”;N$
CALL NAME(N$)
END

SUB NAME(N$)
FOR I = 1 TO 10
PRINT N$
NEXT I
END SUB



9.             Write a program to display series of only odd numbers from 1 to 100 using SUB procedure.
=>
DECLARE SUB SERIES()
REM SERIES OF ODD NUMBERS FROM 1 TO 100
CLS
CALL SERIES
END

SUB SERIES
FOR I = 1 TO 100 STEP 2
PRINT I
NEXT I
END SUB



10.           Write a program to display those numbers which are divisible by 2 and 3 between 1 and 100 using SUB procedure.
=>
DECLARE SUB SERIES
REM TO DISPLAY NUMBERS DIVISIBLE BY 2 & 3 FROM 1 TO 100
CLS
CALL SERIES
END

SUB SERIES
PRINT “The numbers divisible by 2 and 3 from 1 to 100 :”
FOR I = 1 TO 100
IF I MOD 2 = 0 AND I MOD 3 = 0 THEN PRINT I
NEXT I
END SUB



SET-11
1.               Write a SUB program to ask 10 numbers and store in a single dimension array and display the list of input numbers.
=>
DECLARE SUB NUM(N())
REM TO DISPLAY 10 INPUT NUMBERS STORED IN A SINGL;E DIMENSION ARRAY
CLS
DIM N(10)
FOR I = 1 TO 10
INPUT “Enter a number”; N(I)
NEXT I
CALL NUM(N())
END

SUB NUM(N())
CLS
PRINT “The input numbers are:”
FOR I = 1 TO 10
PRINT N(I)
NEXT I
END SUB


2.             Write a SUB program to ask ten numbers and store in single dimension array and find out total prime and composite numbers.
=>
DECLARE SUB COUNT(N())
REM TO FIND THE TOTAL PRIME AND COMPOSITE NUMBERS ENTERED
CLS
DIM N(10)
FOR I = 1 TO 10
INPUT “Enter a number”;N(I)
NEXT I
CALL COUNT(N())
END

SUB COUNT(N())
PC=0
CC=0
FOR I = 1 TO 10
C=0
FOR K=1 TO N(I)
IF N(I) MOD K = 0 THEN
C=C+1
END IF
NEXT K
IF C=2 THEN
PC=PC+1
ELSE
CC=CC+1
END IF
NEXT I
PRINT “Total number of prime numbers:”;PC
PRINT “Total number of composite numbers:”;CC
END SUB


3.              Write a SUB program to ask name of any five items, quantity and their rate in the single dimension array and display list of items, rate and total amount of each item in tabular form.
=>
DECLARE SUB TABLE(N$(), Q(), R())
REM TO DIIPLAY LIST OF ITEMS, RATE AND TOTAL AMOUNT
CLS
DIM N$(5)
DIM Q(5)
DIM R(5)
FOR I = 1 TO 5
INPUT “ENTER NAME OF ITEM”;N$(I)
INPUT “ENTER QUANTITY”;Q(I)
INPUT “ENTER RATE”;R(I)
PRINT
NEXT I
CALL TABLE(N$(), Q(), R())
END

SUB TABLE(N$(), Q(), R())
CLS
PRINT “NAME OF ITEM”, “RATE”, “TOTAL AMOUNT”
FOR I = 1 TO 5
T=R(I)*Q(I)
PRINT N$(I), R(I), T
NEXT I
END SUB



 4.     Write a SUB program to ask 10 numbers and store in a single dimension array and find out roll number is present in array or not.
=>
DECLARE SUB CHECK(N(),R)
REM TO CHECK WHETHER ROLL NUMBER IS PRESENT IN ARRAY OR NOT
CLS
DIM N(10)
FOR I = 1 TO 10
INPUT “Enter any number”;N(I)
NEXT I
CLS
INPUT “Enter your roll number”;R
CALL CHECK(N(),R)
END

SUB CHECK (N (), R)
FLAG = 0
FOR I = 1 TO 10
IF R=N(I) THEN FLAG = 1
NEXT I
IF FLAG = 1 THEN
PRINT "Your roll number  is present in array"
ELSE
PRINT "Your roll number is not present in array"
END IF
END SUB


5.             Write a SUB program to ask ten numbers and store in single dimension array and display in ascending order.
=>
DECLARE SUB ORDER(N())
REM TO DISPLAY INPUT NUMBERS IN ASCENDING ORDER
CLS
DIM N(10)
FOR I = 1 TO 10
INPUT “Enter a number”;N(I)
NEXT I
CALL ORDER(N())
END

SUB ORDER(N())
CLS
PRINT “The numbers in ascending order:”
FOR I = 1 TO 10
FOR K = 1 TO 10-I
IF N(K)>N(K+1) THEN SWAP N(K),N(K+1)
NEXT K
NEXT I

FOR I = 1 TO 10
PRINT N(I)
NEXT I
END SUB


6.             Write a SUB program to as 10 students name and store in single dimension array and search your name is in the list or not.
=>
DECLARE SUB CHECK(N$(),NM$)
REM TO CHECK WHETHER NAME IS PRESENT IN ARRAY OR NOT
CLS
DIM N$(10)
FOR I = 1 TO 10
INPUT “Enter any name”;N$(I)
NEXT I
CLS
INPUT “Enter your name”;NM$
CALL CHECK(N$(),NM$)
END

SUB CHECK (N$(), NM$)
FLAG = 0
FOR I = 1 TO 10
IF UCASE$(NM$) = UCASE$(N$(I)) THEN FLAG = 1
NEXT I
IF FLAG = 1 THEN
PRINT "Your name  is present in array"
ELSE
PRINT "Your name is not present in array"
END IF
END SUB


7.             Write a SUB program to ask ten numbers and store in single dimension array and display in descending order.
=>
DECLARE SUB ORDER(N())
REM TO DISPLAY INPUT NUMBERS IN DESCENDING ORDER
CLS
DIM N(10)
FOR I = 1 TO 10
INPUT “Enter a number”;N(I)
NEXT I
CALL ORDER(N())
END

SUB ORDER(N())
CLS
PRINT “The numbers in descending order:”
FOR I = 1 TO 10
FOR K = 1 TO 10-I
IF N(K)<N(K+1) THEN SWAP N(K),N(K+1)
NEXT K
NEXT I

FOR I = 1 TO 10
PRINT N(I)
NEXT I
END SUB



8.             Write a SUB program to ask ten numbers and store in single dimension array and find out sum, product and difference of greatest and smallest number.
=>
DECLARE SUB SUM(G,S)
DECLARE SUB PRODUCT(G,S)
DECLARE SUB DIFFERENCE(G,S)
REM TO FIND SUM, PRODUCT AND DIFFERENCE OF GREATEST AND SMALLEST NUMBER
CLS
DIM N(10)
FOR I = 1 TO 10
INPUT “Enter any number”;N(I)
NEXT I
G=N(1)
S=N(1)
FOR I = 2 TO 10
IF N(I)>G THEN G=N(I)
IF N(I)<S THEN S=N(I)
NEXT I
CLS
CALL SUM(G,S)
CALL PRODUCT(G,S)
CALL DIFFERENCE(G,S)
END

SUB SUM(G,S)
REM SUM
SM=G+S
PRINT “The sum of greatest and smallest input numbers is:”;SM
END SUB

SUB PRODUCT(G,S)
REM PRODUCT
P=G*S
PRINT “The product of greatest and smallest input numbers is:”;P
END SUB

SUB DIFFERENCE(G,S)
REM DIFFERENCE
D= G-S
PRINT “The difference of greatest and smallest input numbers is:”;D
END SUB



9.             Write a SUB program to ask name of five students and their average marks and display in ascending order in the basis of name.
=>
DECLARE SUB DISPLAY(N$(), AV())
REM TO DISPLAY NAME AND AVERAGE MARSK IN ASCENDING ORDER ON THE BASIS OF NAME
CLS
DIM N$(5)
DIM AV(5)
FOR I = 1 TO 5
INPUT “Enter your name”;N$(I)
INPUT “Enter your average marks”;AV(I)
PRINT
NEXT I
CALL DISPLAY(N$(),AV())
END

SUB DISPLAY(N$(),AV())
CLS
FOR I = 1 TO 5
FOR K = 1 TO 5-I
IF N$(K)>N$(K+1) THEN
SWAP N$(K), N$(K+1)
SWAP AV(K), AV (K+1)
END IF
NEXT K
NEXT I
PRINT “The names and average marks in ascending order”
PRINT “Name”, “Average Marks”
FOR I = 1 TO 5
PRINT N$(I),AV(I)
NEXT I
END SUB



10.           Write a SUB program to ask a number and display its prime factors.
=>
DECLARE SUB PF(N)
REM TO DISPLAY PRIME FACTORS OF GIVEN NUMBERS
CLS
INPUT “Enter any number”;N
CALL PF(N)
END

SUB PF(N)
PRINT  “The prime factors of”;N; ”are:”
FOR I = 2 TO N
IF N  MOD I = 0 THEN
C=0
FOR K = 1 TO N
IF I MOD K = 0 THEN C=C+1
NEXT K
IF C=2 THEN PRINT I
END IF
NEXT I
END SUB

No comments:

Post a Comment