Tuesday, 24 December 2024

Grade-9 Winter Vacation Homework Solved 2081 (Some important)

Grade-9  

Below are the answers to all questions spanning conditional statements (IF, IF...ELSE, IF...ELSEIF...ELSE, SELECT CASE) and library inbuilt functions (LEFT$, RIGHT$, MID$, VAL, STR$, UCASE$, LCASE$). Each question is paired with its solution.


Conditional Statements

1. Check if a number is positive (IF).

Solution:

INPUT "Enter a number: ", num
IF num > 0 THEN
    PRINT "The number is positive."
END IF

2. Check if a number is divisible by 5 (IF).

Solution:

INPUT "Enter a number: ", num
IF num MOD 5 = 0 THEN
    PRINT "The number is divisible by 5."
END IF

3. Check if a number is even or odd (IF...ELSE).

Solution:

INPUT "Enter a number: ", num
IF num MOD 2 = 0 THEN
    PRINT "The number is even."
ELSE
    PRINT "The number is odd."
END IF

4. Determine if the user is an Adult or Minor (IF...ELSE).

Solution:

INPUT "Enter your age: ", age
IF age >= 18 THEN
    PRINT "You are an Adult."
ELSE
    PRINT "You are a Minor."
END IF

5. Grade a student based on marks (IF...ELSEIF...ELSE).

Solution:

INPUT "Enter the marks: ", marks
IF marks >= 90 THEN
    PRINT "Grade: A"
ELSEIF marks >= 80 THEN
    PRINT "Grade: B"
ELSEIF marks >= 70 THEN
    PRINT "Grade: C"
ELSE
    PRINT "Grade: Fail"
END IF

6. Classify BMI categories (IF...ELSEIF...ELSE).

Solution:

INPUT "Enter your BMI: ", bmi
IF bmi < 18.5 THEN
    PRINT "Underweight"
ELSEIF bmi >= 18.5 AND bmi < 25 THEN
    PRINT "Normal weight"
ELSEIF bmi >= 25 AND bmi < 30 THEN
    PRINT "Overweight"
ELSE
    PRINT "Obese"
END IF

7. Display day name using SELECT CASE.

Solution:

INPUT "Enter a number (1-7): ", day
SELECT CASE day
    CASE 1
        PRINT "Monday"
    CASE 2
        PRINT "Tuesday"
    CASE 3
        PRINT "Wednesday"
    CASE 4
        PRINT "Thursday"
    CASE 5
        PRINT "Friday"
    CASE 6
        PRINT "Saturday"
    CASE 7
        PRINT "Sunday"
    CASE ELSE
        PRINT "Invalid day number."
END SELECT

8. Display vehicle category using SELECT CASE.

Solution:

INPUT "Enter the number of wheels: ", wheels
SELECT CASE wheels
    CASE 2
        PRINT "Bike"
    CASE 4
        PRINT "Car"
    CASE IS > 4
        PRINT "Truck"
    CASE ELSE
        PRINT "Unknown vehicle"
END SELECT

9. Check if a character is a vowel or consonant (IF...ELSEIF...ELSE).

Solution:

INPUT "Enter a character: ", char$
char$ = UCASE$(char$) ' Make it case-insensitive
IF INSTR("AEIOU", char$) > 0 THEN
    PRINT "Vowel"
ELSEIF char$ >= "A" AND char$ <= "Z" THEN
    PRINT "Consonant"
ELSE
    PRINT "Invalid input"
END IF

10. Calculate a discount using SELECT CASE.

Solution:

INPUT "Enter the purchase amount: ", amount
SELECT CASE amount
    CASE IS >= 500
        PRINT "Discount: 20%"; " Final amount: "; amount * 0.8
    CASE 200 TO 499
        PRINT "Discount: 10%"; " Final amount: "; amount * 0.9
    CASE IS < 200
        PRINT "No discount"; " Final amount: "; amount
END SELECT

Library Inbuilt Functions

11. Extract the first 3 characters (LEFT$).

Solution:

INPUT "Enter a string: ", userString$
IF LEN(userString$) >= 3 THEN
    PRINT "First 3 characters: "; LEFT$(userString$, 3)
ELSE
    PRINT "String is too short."
END IF

12. Extract the last 4 characters (RIGHT$).

Solution:

INPUT "Enter a string: ", userString$
IF LEN(userString$) >= 4 THEN
    PRINT "Last 4 characters: "; RIGHT$(userString$, 4)
ELSE
    PRINT "String is too short."
END IF

13. Extract a substring (MID$).

Solution:

INPUT "Enter a string: ", userString$
IF LEN(userString$) >= 6 THEN
    PRINT "Substring (3rd to 6th character): "; MID$(userString$, 3, 4)
ELSE
    PRINT "String is too short."
END IF

14. Convert a numeric string to a number (VAL).

Solution:

INPUT "Enter a numeric string: ", numString$
num = VAL(numString$)
PRINT "Result after adding 10: "; num + 10

15. Convert a number to a string (STR$).

Solution:

INPUT "Enter a number: ", num
result$ = STR$(num) + " is the number"
PRINT result$

16. Convert to uppercase (UCASE$).

Solution:

INPUT "Enter a string: ", userString$
PRINT "Uppercase string: "; UCASE$(userString$)

17. Convert to lowercase (LCASE$).

Solution:

INPUT "Enter a string: ", userString$
PRINT "Lowercase string: "; LCASE$(userString$)

18. Check if a string is a palindrome.

Solution:

INPUT "Enter a string: ", userString$
reverseString$ = ""
FOR i = LEN(userString$) TO 1 STEP -1
    reverseString$ = reverseString$ + MID$(userString$, i, 1)
NEXT i

IF UCASE$(userString$) = UCASE$(reverseString$) THEN
    PRINT "The string is a palindrome."
ELSE
    PRINT "The string is not a palindrome."
END IF

19. Count vowels and consonants.

Solution:

REM ' Program to count vowels and consonants using SELECT CASE
INPUT "Enter a string: ", userString$
vowelCount = 0
consonantCount = 0

FOR i = 1 TO LEN(userString$)
    char$ = UCASE$(MID$(userString$, i, 1)) 
    SELECT CASE char$
        CASE "A", "E", "I", "O", "U"
            vowelCount = vowelCount + 1
        CASE ELSE
            consonantCount = consonantCount + 1
    END SELECT
NEXT i

PRINT "Number of vowels: "; vowelCount
PRINT "Number of consonants: "; consonantCount


20. Integer to binary conversion.

Solution:

INPUT "Enter an integer: ", num
binary$ = ""

DO WHILE num > 0
    binary$ = STR$(num MOD 2) + binary$
    num = num \ 2
LOOP

PRINT "Binary representation: "; binary$

This comprehensive set of solved problems demonstrates the usage of conditional statements and library functions in QBasic with practical applications.

No comments:

Post a Comment

Popular Posts