Here is a set of QBasic lab questions and answers that utilize various QBasic library functions such as LCASE$, UCASE$, LEFT$, RIGHT$, MID$, VAL, STR$, INT, CINT, FIX, SIN, COS, TAN, and more. These questions include practical applications like checking for a palindrome and decimal-to-binary conversion.
1. LCASE$ and UCASE$
Q: Write a program to convert a given string to lowercase and uppercase using LCASE$
and UCASE$
.
Answer:
INPUT "Enter a string: ", text$
PRINT "Lowercase: "; LCASE$(text$)
PRINT "Uppercase: "; UCASE$(text$)
2. LEFT$ and RIGHT$
Q: Write a program to extract the first 3 characters and the last 3 characters from a string using LEFT$
and RIGHT$
.
Answer:
INPUT "Enter a string: ", text$
PRINT "First 3 characters: "; LEFT$(text$, 3)
PRINT "Last 3 characters: "; RIGHT$(text$, 3)
3. MID$
Q: Write a program to extract a substring from the 4th to the 7th character of a given string using MID$
.
Answer:
INPUT "Enter a string: ", text$
PRINT "Substring (4th to 7th character): "; MID$(text$, 4, 4)
4. VAL and STR$
Q: Write a program to add two numbers given as strings using VAL
, and convert the result back to a string using STR$
.
Answer:
INPUT "Enter the first number as a string: ", num1$
INPUT "Enter the second number as a string: ", num2$
result = VAL(num1$) + VAL(num2$)
PRINT "The sum is: "; STR$(result)
5. INT, CINT, and FIX
Q: Write a program to demonstrate the difference between INT
, CINT
, and FIX
for a given decimal number.
Answer:
INPUT "Enter a decimal number: ", num
PRINT "INT: "; INT(num)
PRINT "CINT: "; CINT(num)
PRINT "FIX: "; FIX(num)
6. SIN, COS, TAN
Q: Write a program to calculate the sine, cosine, and tangent of an angle (in degrees) using SIN
, COS
, and TAN
.
Answer:
INPUT "Enter an angle in degrees: ", angle
radian = angle * 3.14159 / 180 Convert degrees to radians
PRINT "SIN: "; SIN(radian)
PRINT "COS: "; COS(radian)
PRINT "TAN: "; TAN(radian)
7. Check Palindrome (String Manipulation)
Q: Write a program to check whether a given string is a palindrome.
Answer:
INPUT "Enter a string: ", text$
text$ = UCASE$(text$) REM Convert to uppercase for case-insensitivity
rev$ = ""
FOR i = LEN(text$) TO 1 STEP -1
rev$ = rev$ + MID$(text$, i, 1)
NEXT i
IF text$ = rev$ THEN
PRINT "The string is a palindrome."
ELSE
PRINT "The string is not a palindrome."
END IF
8. Decimal to Binary Conversion
Q: Write a program to convert a decimal number to binary.
Answer:
INPUT "Enter a decimal number: ", decimal
binary$ = ""
DO WHILE decimal > 0
remainder = decimal MOD 2
binary$ = STR$(remainder) + binary$
decimal = decimal \ 2
LOOP
PRINT "The binary equivalent is: "; binary$
9. Calculate the Length of a String
Q: Write a program to calculate and display the length of a string.
Answer:
INPUT "Enter a string: ", text$
PRINT "The length of the string is: "; LEN(text$)
10. Extract the Initials from a Name
Q: Write a program to extract the initials of a full name (e.g., John Doe → J.D.).
Answer:
INPUT "Enter your full name: ", name$
spacePos = INSTR(name$, " ")
initials$ = LEFT$(name$, 1) + "." + MID$(name$, spacePos + 1, 1) + "."
PRINT "Your initials are: "; UCASE$(initials$)
11. Convert Binary to Decimal
Q: Write a program to convert a binary number (as a string) to its decimal equivalent.
Answer:
INPUT "Enter a binary number: ", binary$
decimal = 0
pow=0
FOR i = LEN(binary$) TO 1 STEP -1
digit = VAL(MID$(binary$, i, 1))
decimal = decimal + digit*2^pow
pow=pow+1
NEXT i
PRINT "The decimal equivalent is: "; decimal
12. Sum of ASCII Values of Characters
Q: Write a program to calculate the sum of ASCII values of characters in a string.
Answer:
INPUT "Enter a string: ", text$
sum = 0
FOR i = 1 TO LEN(text$)
sum = sum + ASC(MID$(text$, i, 1))
NEXT i
PRINT "The sum of ASCII values is: "; sum
13. Generate a Cosine Wave Pattern
Q: Write a program to generate a cosine wave pattern.
Answer:
FOR x = 0 TO 360 STEP 10
y = 10 * COS(x * 3.14159 / 180) ' Convert to radians
LOCATE 12, 40 + INT(y)
PRINT "*"
NEXT x
These programs cover a range of functionalities, from string manipulation to mathematical calculations, and utilize a variety of QBasic library functions. This will help students understand and apply these functions in practical scenarios.
No comments:
Post a Comment