1. Fill in the blanks
a. DATA b. infinite loop c. STEP d. true e. pre- f. DO-LOOP WHILE g. nested
2. Write the syntax and purpose of the following statements.
Here’s the syntax, purpose, and example questions for each statement in QBasic:
a. FOR-NEXT
-
Syntax:
FOR Counter = Start TO End [STEP Increment] Statements NEXT Counter
-
Purpose:
Executes a block of code a specific number of times, iterating with a counter. -
Example Question:
Write a program to print the first 10 natural numbers using a FOR-NEXT loop.
b. WHILE-WEND
-
Syntax:
WHILE Condition Statements WEND
-
Purpose:
Repeats a block of code while a condition is true. -
Example Question:
Write a program to print numbers from 1 to 10 using a WHILE-WEND loop.
c. DO-LOOP UNTIL
-
Syntax:
DO Statements LOOP UNTIL Condition
-
Purpose:
Executes a block of code at least once, then repeats until the condition is true. -
Example Question:
Write a program to repeatedly input a number until a positive number is entered.
d. DO-WHILE LOOP
-
Syntax:
DO WHILE Condition Statements LOOP
-
Purpose:
Repeats a block of code while a condition is true. The condition is checked before the loop runs. -
Example Question:
Write a program to print even numbers less than 20 using a DO-WHILE loop.
e. DIM
-
Syntax:
DIM VariableName AS DataType DIM ArrayName(Dimensions)
-
Purpose:
Declares variables or arrays with a specific type or size. -
Example Question:
Declare an array of size 10 to store the names of students.
f. READ-DATA
-
Syntax:
READ Variable DATA Value1, Value2, ...
-
Purpose:
Reads values from aDATA
statement into variables. -
Example Question:
Write a program to read the first three even numbers from a DATA statement and print them.
Each statement's syntax and purpose is tailored to common QBasic programming scenarios and educational examples.
3. Answer the following questions.
a. Define a Control Flow Structure. Write the Name of Basic Control Structures in QBASIC.
-
Definition:
A control flow structure directs the execution order of program statements based on specific conditions or repetition. -
Basic Control Structures in QBASIC:
- Sequential
- Selection (Branching):
IF...THEN
,SELECT CASE
- Iteration (Looping):
FOR-NEXT
,WHILE-WEND
,DO-LOOP
b. What is Branching? Write the Branching Statements Supported by QBASIC.
-
Definition:
Branching is the decision-making process in programming where the control flow takes different paths based on conditions. -
Branching Statements in QBASIC:
IF...THEN
IF...THEN...ELSE
SELECT CASE
c. Differentiate Between Loop Structures. Write the Importance of Loops in Programming.
- Difference Between Loop Structures:
Loop Structure | Description |
---|---|
FOR-NEXT |
Executes a fixed number of iterations based on a counter. |
WHILE-WEND |
Repeats as long as a condition is true, condition checked first. |
DO-WHILE / DO-UNTIL |
Executes repeatedly based on a condition, checked before or after. |
- Importance of Loops:
Loops simplify repetitive tasks, reduce code redundancy, and make programs more efficient.
d. Define Loop Structure. Write the Importance of Loops in Programming.
-
Definition:
A loop structure allows a sequence of instructions to execute repeatedly until a specified condition is met. -
Importance of Loops:
Loops automate repetitive tasks, enhance code efficiency, and simplify programming logic.
e. What is an Infinite Loop? How Do You Break an Infinite Loop?
-
Definition:
An infinite loop is a loop that runs indefinitely because its termination condition is never met. -
Breaking an Infinite Loop:
- Use the
EXIT
statement within the loop. - Example:
DO PRINT "Infinite Loop" EXIT DO ' Break the loop LOOP
- Use the
f. Define Counter or Control Variables.
- Definition:
A counter or control variable is a variable used to track the number of iterations in a loop.- Example:
FOR i = 1 TO 10 PRINT i ' i is the control variable NEXT i
- Example:
g. What is a Pre-Test and Post-Test Looping Statement? Explain with Examples.
-
Pre-Test Looping Statement:
Checks the condition before executing the loop body.
Example:WHILE X <= 10 PRINT X X = X + 1 WEND
-
Post-Test Looping Statement:
Executes the loop body first and then checks the condition.
Example:DO PRINT X X = X + 1 LOOP UNTIL X > 10
h. Define Array. Write Its Advantages.
-
Definition:
An array is a collection of variables of the same data type stored under a single name, accessed using an index.
Example:DIM Marks(5) ' Declares an array of size 5 Marks(1) = 80 PRINT Marks(1)
-
Advantages of Arrays:
- Efficiently stores multiple values under a single name.
- Simplifies code when working with large datasets.
- Provides easy access and manipulation of data using indices.
4. Debug th following programs.
a. REM to print factors of an input number.
INPUT"Enter a number N
FOR K=1 TO 5
IF N MOD K=1 THEN PRINT N
NEXT I
END
b. REM to print multiplication table of an input number.
INPUT"Enter a number........";N
FOR L=1 TO 10 STEP -1
PRINT N; * ; K; "="; N*K
NEXT K
END
Here are the corrected versions of the programs along with explanations of the issues in the original code.
a. REM to Print Factors of an Input Number
Problem in Original Code:
- Incorrect logic in
IF N MOD K = 1
. This condition checks if the remainder is 1, but factors should divide the number completely (N MOD K = 0
). - Incorrect use of
NEXT I
. The loop variable isK
, but theNEXT
statement refers to an undefined variableI
. - Limited the loop to
1 TO 5
, but factors can exceed this range depending on the number.
Corrected Code:
REM Program to Print Factors of an Input Number
INPUT "Enter a number: "; N
FOR K = 1 TO N
IF N MOD K = 0 THEN
PRINT K
END IF
NEXT K
END
Explanation:
- Condition Update: Changed
N MOD K = 1
toN MOD K = 0
to check for factors. - Loop Range: Set
FOR K = 1 TO N
to iterate through all potential factors. - Correct Loop Variable: Changed
NEXT I
toNEXT K
.
b. REM to Print Multiplication Table of an Input Number
Problem in Original Code:
- The loop range is incorrect:
FOR L = 1 TO 10 STEP -1
will not execute because the step is negative, but the range starts with a smaller value. - Incorrect variable in the multiplication statement:
N * K
usesK
, which is not declared or incremented.
Corrected Code:
REM Program to Print Multiplication Table of an Input Number
INPUT "Enter a number: "; N
FOR L = 1 TO 10
PRINT N; " * "; L; " = "; N * L
NEXT L
END
Explanation:
- Correct Loop Range: Changed
STEP -1
to a standard increment by default (STEP 1
is implicit). - Correct Variable: Replaced
K
withL
in the multiplication (N * L
).
5. Rewrite the following program using WHILE-WEND and DO-LOOP UNTIL statements.
Here are the given programs rewritten using WHILE-WEND
and DO-LOOP UNTIL
:
a. REM to Repeat Loop Ten Times
Using WHILE-WEND:
REM Program to Repeat Loop Ten Times Using WHILE-WEND
CLS
I = 10
WHILE I >= 1
PRINT "Loop is repeating "; I; " time"
I = I - 1
WEND
PRINT "End of Loop"
END
Using DO-LOOP UNTIL:
REM Program to Repeat Loop Ten Times Using DO-LOOP UNTIL
CLS
I = 10
DO
PRINT "Loop is repeating "; I; " time"
I = I - 1
LOOP UNTIL I < 1
PRINT "End of Loop"
END
b. REM Example of Loop (Print Cubes of Even Numbers)
Using WHILE-WEND:
REM Example of Loop Using WHILE-WEND
CLS
N = 2
WHILE N <= 20
PRINT N ^ 3
N = N + 2
WEND
PRINT "Loop Ended"
END
Using DO-LOOP UNTIL:
REM Example of Loop Using DO-LOOP UNTIL
CLS
N = 2
DO
PRINT N ^ 3
N = N + 2
LOOP UNTIL N > 20
PRINT "Loop Ended"
END
Explanation:
-
WHILE-WEND
:- The condition is checked before the loop executes.
- If the condition is false initially, the loop does not run.
-
DO-LOOP UNTIL
:- The loop executes at least once before checking the condition at the end.
Both implementations achieve the same logic as the original programs but use different looping mechanisms.
6. Type the following programs and then write the output of each program with the help of dry run.
Sure! Below are the dry runs of each program in the form of tables for better understanding.
a. Find the Divisors of a Given Number
Code:
REM Find the Divisors of a Number
CLS
INPUT "Enter a number:::::"; N
C = 1
WHILE C <= N
R = N MOD C
IF R = 0 THEN
PRINT C
END IF
C = C + 1
WEND
END
Dry Run Table:
Iteration | C | R = N MOD C | Action | Output |
---|---|---|---|---|
1 | 1 | 12 MOD 1 = 0 | Print 1 |
1 |
2 | 2 | 12 MOD 2 = 0 | Print 2 |
2 |
3 | 3 | 12 MOD 3 = 0 | Print 3 |
3 |
4 | 4 | 12 MOD 4 = 0 | Print 4 |
4 |
5 | 5 | 12 MOD 5 = 2 | No print | |
6 | 6 | 12 MOD 6 = 0 | Print 6 |
6 |
7 | 7 | 12 MOD 7 = 5 | No print | |
8 | 8 | 12 MOD 8 = 4 | No print | |
9 | 9 | 12 MOD 9 = 3 | No print | |
10 | 10 | 12 MOD 10 = 2 | No print | |
11 | 11 | 12 MOD 11 = 1 | No print | |
12 | 12 | 12 MOD 12 = 0 | Print 12 |
12 |
Final Output:
1
2
3
4
6
12
b. Find Factorial of a Number
Code:
REM Find the Factorial of a Number
CLS
INPUT "Enter a number to find factorial::::::"; N
C = 1
F = 1
DO
F = F * C
C = C + 1
LOOP WHILE C <= N
PRINT "Factorial of "; N; "="; F
END
Dry Run Table:
Iteration | C | F (F = F * C) | Action | F (Factorial) |
---|---|---|---|---|
1 | 1 | 1 * 1 = 1 | Multiply | 1 |
2 | 2 | 1 * 2 = 2 | Multiply | 2 |
3 | 3 | 2 * 3 = 6 | Multiply | 6 |
4 | 4 | 6 * 4 = 24 | Multiply | 24 |
5 | 5 | 24 * 5 = 120 | Multiply | 120 |
Final Output:
Factorial of 5 = 120
c. Find the Greatest Number from 10 Inputs
Code:
REM Find the Greatest Number from 10 Inputs
CLS
C = 1
INPUT "Enter next number:::::"; G
DO
INPUT "Enter next number::::"; N
IF N > G THEN
G = N
END IF
C = C + 1
IF C = 10 THEN NEXT DO
LOOP
PRINT "Greatest Number is "; G
END
Dry Run Table (Assuming Inputs: 5, 7, 3, 9, 10, 2, 8, 4, 11, 6
):
Iteration | N (Input) | G (Greatest So Far) | C (Counter) | Action |
---|---|---|---|---|
1 | 5 | 5 | 1 | G = 5 (initial) |
2 | 7 | 7 | 2 | G = 7 |
3 | 3 | 7 | 3 | No change |
4 | 9 | 9 | 4 | G = 9 |
5 | 10 | 10 | 5 | G = 10 |
6 | 2 | 10 | 6 | No change |
7 | 8 | 10 | 7 | No change |
8 | 4 | 10 | 8 | No change |
9 | 11 | 11 | 9 | G = 11 |
10 | 6 | 11 | 10 | No change |
Final Output:
Greatest Number is 11
d. Check if a Number is Prime or Composite
Code:
REM Check if a number is Prime or Composite
TOP:
CLS
INPUT "Enter a Number Bigger than 1:::::"; N
IF N <= 1 THEN GOTO TOP
FLAG = 0
FOR K = 2 TO N / 2
R = N MOD K
IF R = 0 THEN
FLAG = 1
EXIT FOR
END IF
NEXT
IF FLAG = 1 THEN
PRINT N; " IS COMPOSITE NUMBER"
ELSE
PRINT N; " IS PRIME NUMBER"
END IF
END
Dry Run Table (Example 1: N = 13
):
Iteration | K | R = N MOD K | Action | Flag | Output |
---|---|---|---|---|---|
1 | 2 | 13 MOD 2 = 1 | Continue loop | 0 | |
2 | 3 | 13 MOD 3 = 1 | Continue loop | 0 | |
3 | 4 | 13 MOD 4 = 1 | Continue loop | 0 | |
4 | 5 | 13 MOD 5 = 3 | Continue loop | 0 | |
5 | 6 | 13 MOD 6 = 1 | Continue loop | 0 | |
6 | 7 | 13 MOD 7 = 6 | Continue loop | 0 |
Final Output (Since no divisor was found):
13 IS PRIME NUMBER
Dry Run Table (Example 2: N = 12
):
Iteration | K | R = N MOD K | Action | Flag | Output |
---|---|---|---|---|---|
1 | 2 | 12 MOD 2 = 0 | Flag = 1, exit loop | 1 | |
2 | 3 | 12 MOD 3 = 0 | Exit loop | 1 |
Final Output (Since a divisor was found):
12 IS COMPOSITE NUMBER
Summary of Outputs:
-
a: Finds divisors:
1 2 3 4 6 12
-
b: Finds factorial of a number:
Factorial of 5 = 120
-
c: Finds the greatest number from 10 inputs:
Greatest Number is 11
-
d: Checks if a number is prime or composite:
- For
N = 13
:13 IS PRIME NUMBER
- For
N = 12
:12 IS COMPOSITE NUMBER
- For
7. Write the programs to print the following outputs using nested loop:
To generate the given patterns in QBasic using nested FOR loops, I'll break down the required program for each pattern. These patterns are made using loops that adjust the printed values accordingly.
a. Pattern:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
QBasic Code for Pattern a:
REM Pattern a
CLS
FOR i = 5 TO 1 STEP -1
FOR j = 1 TO i
PRINT j; " ";
NEXT j
PRINT
NEXT i
END
Explanation:
- The outer loop (
FOR i = 5 TO 1 STEP -1
) controls the number of columns, starting at 5 and decreasing each time. - The inner loop (
FOR j = 1 TO i
) prints numbers from1
toi
. - After each line is printed,
PRINT
moves to the next line.
b. Pattern:
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
QBasic Code for Pattern b:
REM Pattern b
CLS
FOR i = 5 TO 1 STEP -1
FOR j = 1 TO i
PRINT i; " ";
NEXT j
PRINT
NEXT i
END
Explanation:
- The outer loop (
FOR i = 5 TO 1 STEP -1
) controls the number to be printed, starting from 5 down to 1. - The inner loop (
FOR j = 1 TO i
) prints the current value ofi
multiple times. - After each line, the program moves to the next line with
PRINT
.
c. Pattern:
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
QBasic Code for Pattern c:
REM Pattern c
CLS
FOR i = 5 TO 1 STEP -1
FOR j = 5 TO i STEP -1
PRINT j; " ";
NEXT j
PRINT
NEXT i
END
Explanation:
- The outer loop (
FOR i = 5 TO 1 STEP -1
) is responsible for controlling the number of rows. - The inner loop (
FOR j = 5 TO i STEP -1
) prints numbers starting from 5 and decreasing toi
. - After each row is printed,
PRINT
moves to the next line.
d. Pattern:
1
121
12321
1234321
123454321
QBasic Code for Pattern d:
REM Pattern d
CLS
FOR i = 1 TO 5
REM Print increasing numbers
FOR j = 1 TO i
PRINT j; " ";
NEXT j
REM Print decreasing numbers
FOR j = i - 1 TO 1 STEP -1
PRINT j; " ";
NEXT j
PRINT
NEXT i
END
Explanation:
- The outer loop (
FOR i = 1 TO 5
) controls the number of rows. - The first inner loop (
FOR j = 1 TO i
) prints the numbers in increasing order. - The second inner loop (
FOR j = i - 1 TO 1 STEP -1
) prints the numbers in decreasing order after reaching the peak value. - After each row,
PRINT
moves to the next line.
8. Write the following program and answer the given questions:
Let's analyze the given QBasic program step by step, and address each part of your query.
QBasic Program:
CLS
S = 0
L = 1
WHILE L <= 10
READ N
PRINT N
S = S + N
L = L + 1
WEND
A = S / 10
PRINT "Average="; A
DATA 3, 4, 5, 2, 3, 6, 7, 8, 5, 6
END
i. List the variables and operators used in the program:
Variables:
- S: Used to accumulate the sum of the numbers.
- L: Loop control variable, used to count iterations.
- N: Holds each value read from the
DATA
statement. - A: Holds the average value, calculated after the loop.
Operators:
- =: Assignment operator, used to assign values to variables (e.g.,
S = 0
). - +: Addition operator, used to add the value of
N
to the sum (S = S + N
). - / (Division): Division operator, used to calculate the average (
A = S / 10
). - <=: Less than or equal to operator, used to check the loop condition (
L <= 10
).
ii. What happens if you remove the L = L + 1
line from the program?
If you remove the line L = L + 1
, the program will enter an infinite loop. Here's why:
- The
L
variable is being used as a counter in theWHILE
loop to ensure the loop runs 10 times. - Without
L = L + 1
, the value ofL
will always remain the same (initially1
). - The condition
L <= 10
will always be true becauseL
will never increase, and the loop will run indefinitely. - As a result, the program will keep reading the same value from
DATA
, printing it, and adding it to the sumS
, but it will never exit the loop, causing the program to run forever.
iii. Rewrite the program using the FOR-NEXT
statement:
You can replace the WHILE
loop with a FOR-NEXT
loop. The FOR-NEXT
loop automatically handles the loop counter, so you don’t need to manually update L
. Here's the rewritten version of the program:
CLS
S = 0
FOR L = 1 TO 10
READ N
PRINT N
S = S + N
NEXT L
A = S / 10
PRINT "Average="; A
DATA 3, 4, 5, 2, 3, 6, 7, 8, 5, 6
END
Explanation of the FOR-NEXT
loop:
FOR L = 1 TO 10
: This specifies that the loop will run 10 times, fromL = 1
toL = 10
.- Inside the loop:
READ N
: Reads a number from theDATA
statement.PRINT N
: Prints the value ofN
.S = S + N
: Adds the value ofN
to the sumS
.
- The
NEXT L
statement automatically increments the value ofL
after each iteration, and the loop ends after 10 iterations.
This is a more concise and efficient way to write the program, as you no longer need to manually increment the loop control variable (L
) within the loop.
9. Rewrite the following program using WHILE-WNED :
To rewrite the given program using a WHILE-WEND loop, we need to simulate the behavior of nested FOR
loops using a WHILE
loop. In the original FOR
loop, the outer loop runs K
from 1 to 5, and the inner loop runs J
from 1 to K
. In each inner loop, it prints both J
and J + K
. We'll mimic this using a WHILE-WEND
loop.
Original Program Using FOR-NEXT:
FOR K = 1 TO 5
FOR J = 1 TO K
PRINT J, J + K
NEXT J
PRINT
NEXT K
END
Rewritten Program Using WHILE-WEND:
CLS
K = 1
WHILE K <= 5
J = 1
WHILE J <= K
PRINT J, J + K
J = J + 1
WEND
PRINT
K = K + 1
WEND
END
Explanation:
-
Outer Loop (K):
- We initialize
K
to 1 and use aWHILE
loop to repeat the process as long asK <= 5
. - After completing the inner loop, we increment
K
by 1 usingK = K + 1
.
- We initialize
-
Inner Loop (J):
- Inside the outer loop, we initialize
J
to 1 for each value ofK
. - The inner loop runs while
J <= K
. In each iteration, it printsJ
andJ + K
. - After each inner loop iteration, we increment
J
by 1 usingJ = J + 1
.
- Inside the outer loop, we initialize
-
PRINT:
- After finishing the inner loop for each value of
K
, aPRINT
statement is used to move to the next line.
- After finishing the inner loop for each value of
Output:
The output of this program will be the same as the original one:
1 2
1 3 2 4
1 4 2 5 3 6
1 5 2 6 3 7 4 8
1 6 2 7 3 8 4 9 5 10
This program correctly replicates the behavior of the original FOR-NEXT
program using WHILE-WEND
loops.
10. Write programs to print the following series:
To print the given series using QBasic, we can write programs with appropriate loop structures for each pattern. Below are the QBasic programs for each series:
a. Series: 1, 2, 3, ..., up to 10 terms
CLS
FOR i = 1 TO 10
PRINT i;
NEXT i
END
b. Series: 100, 95, 90, ..., up to 10th terms
CLS
FOR i = 100 TO 51 STEP -5
PRINT i;
NEXT i
END
c. Series: 1, 4, 9, ..., up to 10 terms
CLS
FOR i = 1 TO 10
PRINT i * i;
NEXT i
END
d. Series: 1, 5, 9, 13, ..., up to 20th terms
CLS
FOR i = 1 TO 20 STEP 4
PRINT i;
NEXT i
END
e. Series: 2, 8, 18, 32, ..., up to 10th terms
CLS
FOR i = 1 TO 10
PRINT i * i * 2;
NEXT i
END
f. Series: 1, 1, 2, 3, 5, 8, ..., up to 10th terms (Fibonacci series)
CLS
a = 1
b = 1
PRINT a;
PRINT b;
FOR i = 3 TO 10
c = a + b
PRINT c;
a = b
b = c
NEXT i
END
g. Series: 2, 8, 15, 23, 32, ..., up to 10th terms
CLS
FOR i = 1 TO 10
PRINT (i * i) + 1;
NEXT i
END
h. Series: 9, 28, 14, 7, 22, ..., until the number is 1
CLS
n = 9
WHILE n > 1
PRINT n;
IF n MOD 2 = 0 THEN
n = n / 2
ELSE
n = n - 1
END IF
WEND
END
i. Series: 7, 22, 11, 34, ..., up to 10th terms
CLS
n = 7
FOR i = 1 TO 10
PRINT n;
IF i MOD 2 = 1 THEN
n = n + 15
ELSE
n = n - 11
END IF
NEXT i
END
j. Series: 1, 2, 4, 7, 11, ..., up to 10th terms
CLS
n = 1
FOR i = 1 TO 10
PRINT n;
n = n + i
NEXT i
END
Explanation:
- Series (a): Prints the first 10 natural numbers starting from 1.
- Series (b): Starts at 100 and decreases by 5 each time, printing up to the 10th term.
- Series (c): Prints squares of the numbers from 1 to 10.
- Series (d): Prints numbers starting from 1, increasing by 4 each time.
- Series (e): Prints twice the square of numbers from 1 to 10.
- Series (f): Prints the Fibonacci sequence, which is generated by adding the previous two numbers in the series.
- Series (g): Prints a sequence based on the formula
i * i + 1
. - Series (h): Prints a number sequence, halving the number if it's even, or subtracting 1 if it's odd, until the number becomes 1.
- Series (i): Starts with 7, adds 15 on odd iterations, and subtracts 11 on even iterations.
- Series (j): Prints numbers starting from 1, with the difference between consecutive terms increasing by 1.
You can run these programs in QBasic to print the respective series. Each loop structure and sequence generation mechanism matches the desired output.
11. Write program codes for the following:
Here are the QBasic programs for each of the tasks mentioned:
a. To input 10 different numbers and then print the smallest number.
CLS
DIM numbers(10)
FOR i = 1 TO 10
INPUT "Enter a number: "; numbers(i)
NEXT i
smallest = numbers(1)
FOR i = 2 TO 10
IF numbers(i) < smallest THEN
smallest = numbers(i)
END IF
NEXT i
PRINT "The smallest number is: "; smallest
END
b. To input 15 different names and then print the longest name.
CLS
DIM names(15)
FOR i = 1 TO 15
INPUT "Enter a name: "; names(i)
NEXT i
longestName = names(1)
FOR i = 2 TO 15
IF LEN(names(i)) > LEN(longestName) THEN
longestName = names(i)
END IF
NEXT i
PRINT "The longest name is: "; longestName
END
c. To print even numbers from 2 to 50.
CLS
FOR i = 2 TO 50 STEP 2
PRINT i
NEXT i
END
d. To input a number and print whether the number is Prime or Composite.
CLS
INPUT "Enter a number: "; N
IF N <= 1 THEN
PRINT "Neither Prime nor Composite"
END
END IF
isPrime = TRUE
FOR i = 2 TO INT(SQR(N))
IF N MOD i = 0 THEN
isPrime = FALSE
EXIT FOR
END IF
NEXT i
IF isPrime THEN
PRINT N; " is a Prime number"
ELSE
PRINT N; " is a Composite number"
END IF
END
e. To print only the prime numbers between 5 and 50.
CLS
FOR N = 5 TO 50
isPrime = TRUE
FOR i = 2 TO INT(SQR(N))
IF N MOD i = 0 THEN
isPrime = FALSE
EXIT FOR
END IF
NEXT i
IF isPrime THEN
PRINT N
END IF
NEXT N
END
f. To input 10 different numbers and then print them in descending order.
CLS
DIM numbers(10)
FOR i = 1 TO 10
INPUT "Enter a number: "; numbers(i)
NEXT i
REM Sorting numbers in descending order (Bubble Sort)
FOR i = 1 TO 9
FOR j = i + 1 TO 10
IF numbers(i) < numbers(j) THEN
temp = numbers(i)
numbers(i) = numbers(j)
numbers(j) = temp
END IF
NEXT j
NEXT i
PRINT "The numbers in descending order are:"
FOR i = 1 TO 10
PRINT numbers(i)
NEXT i
END
Explanation:
-
Program a:
- Takes 10 numbers as input and stores them in an array.
- Finds the smallest number by comparing each input number.
-
Program b:
- Takes 15 names as input and stores them in an array.
- Finds the longest name by comparing the lengths of each input name.
-
Program c:
- Prints even numbers from 2 to 50 using a
FOR
loop with a step of 2.
- Prints even numbers from 2 to 50 using a
-
Program d:
- Takes a number as input and checks if it's prime or composite by attempting to divide it with numbers from 2 to the square root of the input number.
- A prime number has no divisors other than 1 and itself.
-
Program e:
- Loops through the numbers between 5 and 50, checks if each number is prime, and prints it if it is.
-
Program f:
- Takes 10 numbers as input and stores them in an array.
- Sorts the numbers in descending order using a Bubble Sort algorithm and then prints them.
No comments:
Post a Comment