Thursday, 26 December 2024

Grade-9 Ch-18: Control Structure Lab

 Here is a set of lab questions with sample answers for students in computer science, focused on QBasic programming. The topics include if, if..else, if..else if..else, select case, goto, for, while, do while, do...loop while, do until, do...loop until, along with pattern generation and series programs like Hailstone and Armstrong numbers.


1. If Statement

Q: Write a QBasic program to check if a number is positive, negative, or zero using the if statement.

Answer:

INPUT "Enter a number: ", num
IF num > 0 THEN
    PRINT "The number is positive."
ELSEIF num < 0 THEN
    PRINT "The number is negative."
ELSE
    PRINT "The number is zero."
END IF

2. If...Else Statement

Q: Write a program to determine whether a number is even or odd using if...else.

Answer:

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

3. If...Else If...Else Statement

Q: Write a program to assign grades based on marks:

  • >= 90: Grade A
  • >= 80: Grade B
  • >= 70: Grade C
  • >= 60: Grade D
  • < 60: Grade F

Answer:

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"
ELSEIF marks >= 60 THEN
    PRINT "Grade: D"
ELSE
    PRINT "Grade: F"
END IF

4. Select Case Statement

Q: Write a program to display the day of the week based on user input (1-7).

Answer:

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

5. Goto Statement

Q: Write a program to calculate the sum of two numbers. If the result exceeds 100, jump to a label and display a warning.

Answer:

INPUT "Enter first number: ", a
INPUT "Enter second number: ", b
sum = a + b
IF sum > 100 THEN
    GOTO Warning
END IF
PRINT "The sum is: "; sum
END

Warning:
PRINT "Warning: Sum exceeds 100!"

6. For Loop

Q: Write a program to generate a multiplication table for a given number.

Answer:

INPUT "Enter a number: ", num
FOR i = 1 TO 10
    PRINT num; " x "; i; " = "; num * i
NEXT i

7. While Loop

Q: Write a program to calculate the factorial of a number using a while loop.

Answer:

INPUT "Enter a number: ", num
fact = 1
i = 1
WHILE i <= num
    fact = fact * i
    i = i + 1
WEND
PRINT "The factorial is: "; fact

8. Do While Loop

Q: Write a program to find the sum of digits of a number using a do while loop.

Answer:

INPUT "Enter a number: ", num
sum = 0
DO WHILE num > 0
    digit = num MOD 10
    sum = sum + digit
    num = num \ 10
LOOP
PRINT "The sum of digits is: "; sum

9. Do...Loop While

Q: Write a program to calculate the sum of natural numbers up to n using do...loop while.

Answer:

INPUT "Enter a number: ", n
sum = 0
i = 1
DO
    sum = sum + i
    i = i + 1
LOOP WHILE i <= n
PRINT "The sum is: "; sum

10. Do Until Loop

Q: Write a program to reverse a number using a do until loop.

Answer:

INPUT "Enter a number: ", num
rev = 0
DO UNTIL num = 0
    digit = num MOD 10
    rev = rev * 10 + digit
    num = num \ 10
LOOP
PRINT "The reversed number is: "; rev

11. Do...Loop Until

Q: Write a program to find the sum of even numbers up to n using do...loop until.

Answer:

INPUT "Enter a number: ", n
sum = 0
i = 2
DO
    sum = sum + i
    i = i + 2
LOOP UNTIL i > n
PRINT "The sum of even numbers is: "; sum

12. Pattern Generation

Q: Write a program to generate the following pattern:

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

Answer:

INPUT "Enter the number of rows: ", rows
FOR i = 1 TO rows
    FOR j = 1 TO i
        PRINT "*";
    NEXT j
    PRINT
NEXT i

13. Hailstone Series

Q: Write a program to generate the Hailstone series for a given number 

7,22,11,34,17,52,26,13,40,207, 22, 11, 34, 17, 52, 26, 13, 40, 20

Answer:


CLS
PRINT "Hailstone Series (10 terms):"
n=7
FOR i = 1 TO 10
    PRINT n; " ";
    IF n MOD 2 = 0 THEN
        n = n / 2 REMIf even, divide by 2
    ELSE
        n = 3 * n + 1 REM If odd, multiply by 3 and add 1
    END IF
NEXT i
PRINT
END 

14. Armstrong Number

Q: Write a program to check whether a given number is an Armstrong number.

Answer:

INPUT "Enter a number: ", num
original = num
sum = 0
DO WHILE num > 0
    digit = num MOD 10
    sum = sum + digit ^ 3
    num = num \ 10
LOOP
IF sum = original THEN
    PRINT original; " is an Armstrong number."
ELSE
    PRINT original; " is not an Armstrong number."
END IF



13. Write a program to generate the series for a given number 

1, 1, 2, 3, 5........................upto 10th Term

Answer:

CLS PRINT "Series for (10 terms):" a=1

b=1

PRINT a;b; FOR i = 1 TO 8

c=a+b

PRINT c;

a=b

b=c NEXT i PRINT END 


These questions provide a mix of logic building, control structure usage, and basic problem-solving for students to practice QBasic programming effectively.

No comments:

Post a Comment

Popular Posts