Sunday, 29 December 2024

Grade-10 Ch-9: Review of loop structure and array lab work RoseBud

 Here are 10 QBasic lab work questions based on the different loop structures and one-dimensional arrays, with solutions for each:


1. Write a program to print numbers from 1 to 10 using the FOR-NEXT loop.

Solution:

CLS
FOR i = 1 TO 10
    PRINT i
NEXT i
END

Explanation:

  • The FOR-NEXT loop runs from 1 to 10 and prints each value of i.

2. Write a program to calculate the sum of numbers from 1 to 50 using the FOR-NEXT loop.

Solution:

CLS
sum = 0
FOR i = 1 TO 50
    sum = sum + i
NEXT i
PRINT "Sum of numbers from 1 to 50 is: "; sum
END

Explanation:

  • The FOR-NEXT loop adds the numbers from 1 to 50 and stores the sum in the variable sum.

3. Write a program to print all even numbers between 1 and 20 using the WHILE-WEND loop.

Solution:

CLS
n = 2
WHILE n <= 20
    PRINT n
    n = n + 2
WEND
END

Explanation:

  • The WHILE-WEND loop starts with 2 and increments by 2, printing all even numbers from 2 to 20.

4. Write a program to calculate the factorial of a number using the DO WHILE...LOOP structure.

Solution:

CLS
INPUT "Enter a number: ", N
fact = 1
i = 1
DO WHILE i <= N
    fact = fact * i
    i = i + 1
LOOP
PRINT "The factorial of "; N; " is "; fact
END

Explanation:

  • The DO WHILE...LOOP calculates the factorial of the number N by multiplying the numbers from 1 to N.

5. Write a program to print numbers from 10 down to 1 using the DO...LOOP WHILE structure.

Solution:

CLS
i = 10
DO
    PRINT i
    i = i - 1
LOOP WHILE i >= 1
END

Explanation:

  • The DO...LOOP WHILE loop prints numbers from 10 down to 1, decreasing i with each iteration.

6. Write a program to print the multiplication table of a number entered by the user using the DO...LOOP UNTIL structure.

Solution:

CLS
INPUT "Enter a number: ", N
i = 1
DO
    PRINT N; " * "; i; " = "; N * i
    i = i + 1
LOOP UNTIL i > 10
END

Explanation:

  • The DO...LOOP UNTIL loop prints the multiplication table of the number N up to 10.

7. Write a program to sum the numbers from 1 to 100 using a FOR-NEXT loop and a one-dimensional array to store the numbers.

Solution:

CLS
DIM numbers(100)
sum = 0
FOR i = 1 TO 100
    numbers(i) = i
    sum = sum + numbers(i)
NEXT i
PRINT "Sum of numbers from 1 to 100 is: "; sum
END

Explanation:

  • This program uses a one-dimensional array numbers to store values from 1 to 100 and then calculates their sum.

8. Write a program to input 5 numbers into an array and print them in reverse order using the FOR-NEXT loop.

Solution:

CLS
DIM nums(5)
FOR i = 1 TO 5
    INPUT "Enter a number: ", nums(i)
NEXT i

PRINT "Numbers in reverse order:"
FOR i = 5 TO 1 STEP -1
    PRINT nums(i)
NEXT i
END

Explanation:

  • This program takes 5 numbers as input and prints them in reverse order using the FOR-NEXT loop.

9. Write a program to check if a number entered by the user is a prime number using a WHILE-WEND loop.

Solution:

CLS
INPUT "Enter a number: ", N
IF N <= 1 THEN
    PRINT "Not a prime number."
    END
END IF
i = 2
isPrime = TRUE
WHILE i <= INT(SQR(N))
    IF N MOD i = 0 THEN
        isPrime = FALSE
        EXIT WHILE
    END IF
    i = i + 1
WEND
IF isPrime THEN
    PRINT N; " is a prime number."
ELSE
    PRINT N; " is not a prime number."
END IF
END

Explanation:

  • The program checks if a number is prime by dividing it by all numbers from 2 to the square root of N.

10. Write a program to reverse the elements of a one-dimensional array using the DO...LOOP UNTIL structure.

Solution:

CLS
DIM arr(5)
FOR i = 1 TO 5
    INPUT "Enter a number: ", arr(i)
NEXT i

i = 1
j = 5
DO
    temp = arr(i)
    arr(i) = arr(j)
    arr(j) = temp
    i = i + 1
    j = j - 1
LOOP UNTIL i >= j

PRINT "Array in reversed order:"
FOR i = 1 TO 5
    PRINT arr(i)
NEXT i
END

Explanation:

  • This program reverses the elements of a one-dimensional array using the DO...LOOP UNTIL structure by swapping elements from both ends of the array.

Conclusion:

These questions help practice using the different loop structures (FOR-NEXT, WHILE-WEND, DO WHILE...LOOP, DO...LOOP UNTIL) and one-dimensional arrays in QBasic. Each solution demonstrates a basic application of these structures to handle loops and arrays in various scenarios.

No comments:

Post a Comment

Popular Posts