Sunday, 29 December 2024

Grade-10 Ch:10 Review of built in functions in QBASIC

 Below is a QBASIC lab assignment that demonstrates the use of the string and numeric functions: LEFT$, RIGHT$, MID$, UCASE$, LCASE$, VAL, STR$, INT, CINT, FIX, SIN, and COS. Each function is explained with a program.


Lab Assignment Questions and Programs

1. LEFT$, RIGHT$, MID$ Functions

Question: Write a program in QBASIC to extract and display parts of a string using LEFT$, RIGHT$, and MID$.

Program:

PRINT "Enter a string: ";
INPUT str$
PRINT "First 3 characters: "; LEFT$(str$, 3)
PRINT "Last 3 characters: "; RIGHT$(str$, 3)
PRINT "Characters from position 4 to 6: "; MID$(str$, 4, 3)

2. UCASE$, LCASE$ Functions

Question: Write a program to convert a string to uppercase and lowercase using UCASE$ and LCASE$.

Program:

PRINT "Enter a string: ";
INPUT str$
PRINT "Uppercase: "; UCASE$(str$)
PRINT "Lowercase: "; LCASE$(str$)

3. VAL and STR$ Functions

Question: Write a program to convert a string to a number using VAL and convert a number to a string using STR$.

Program:

PRINT "Enter a numeric string: ";
INPUT numStr$
num = VAL(numStr$)
PRINT "The number is: "; num

PRINT "Enter a number: ";
INPUT num
str$ = STR$(num)
PRINT "The string representation is: "; str$

4. INT, CINT, and FIX Functions

Question: Write a program to demonstrate the difference between INT, CINT, and FIX functions.

Program:

PRINT "Enter a decimal number: ";
INPUT num

PRINT "INT (Truncates towards negative infinity): "; INT(num)
PRINT "CINT (Rounds to the nearest integer): "; CINT(num)
PRINT "FIX (Truncates towards zero): "; FIX(num)

5. SIN and COS Functions

Question: Write a program to calculate the sine and cosine of an angle in degrees using SIN and COS.

Program:

PRINT "Enter an angle in degrees: ";
INPUT angle
rad = angle * 3.14159 / 180 ' Convert degrees to radians
PRINT "Sine of "; angle; " degrees: "; SIN(rad)
PRINT "Cosine of "; angle; " degrees: "; COS(rad)

Explanation of Functions

  1. LEFT$(string, length): Extracts the leftmost length characters of the string.
  2. RIGHT$(string, length): Extracts the rightmost length characters of the string.
  3. MID$(string, start, length): Extracts length characters starting from position start.
  4. UCASE$(string): Converts the string to uppercase.
  5. LCASE$(string): Converts the string to lowercase.
  6. VAL(string): Converts a numeric string into a number.
  7. STR$(number): Converts a number into a string.
  8. INT(number): Truncates a number toward negative infinity.
  9. CINT(number): Rounds a number to the nearest integer.
  10. FIX(number): Truncates a number toward zero.
  11. SIN(angle_in_radians): Returns the sine of the angle (angle must be in radians).
  12. COS(angle_in_radians): Returns the cosine of the angle (angle must be in radians).

Sample Lab Output

Program 1:

Input: Programming
Output:

First 3 characters: Pro
Last 3 characters: ing
Characters from position 4 to 6: gra

Program 2:

Input: hello world
Output:

Uppercase: HELLO WORLD
Lowercase: hello world

Program 5:

Input: 60 degrees
Output:

Sine of 60 degrees: 0.866025
Cosine of 60 degrees: 0.5

These programs are excellent practice for understanding how to manipulate strings and perform numeric operations in QBASIC. Let me know if you need additional explanations or enhancements!

Additional Questions


Lab Question: Checking if a String is Palindrome Using MID$

Question:
Write a QBASIC program to check if the entered string is a palindrome or not. Use the MID$ function to reverse the string and compare it with the original.


Program:

PRINT "Enter a string: "
INPUT str$
len = LEN(str$)
reversed$ = ""

REM Reverse the string using MID$
FOR i = len TO 1 STEP -1
    reversed$ = reversed$ + MID$(str$, i, 1)
NEXT i

REM Check if the string is a palindrome
IF UCASE$(str$) = UCASE$(reversed$) THEN
    PRINT "The string is a palindrome."
ELSE
    PRINT "The string is not a palindrome."
END IF

Explanation of the Code:

  1. Input String:

    • The program asks the user to input a string.
  2. Reverse the String:

    • The MID$ function is used to extract each character from the string starting from the end and append it to a new string (reversed$).
  3. Compare Strings:

    • The program compares the original string with its reversed version using UCASE$ to ensure the comparison is case-insensitive.
  4. Result:

    • If the original and reversed strings match, the string is a palindrome.

Sample Output:

Test Case 1:

Input: madam
Output:

The string is a palindrome.

Test Case 2:

Input: hello
Output:

The string is not a palindrome.

This program effectively demonstrates string manipulation using MID$ and teaches students how to work with loops and conditionals in QBASIC.

No comments:

Post a Comment

Popular Posts