0% found this document useful (0 votes)
6 views28 pages

QBASIC Programming Loops and Examples

The document provides a series of QBASIC programming examples demonstrating the use of FOR NEXT and WHILE WEND loops. It includes programs for printing even and odd numbers, calculating averages, factorials, and checking for prime numbers, along with explanations and dry runs for each example. Additionally, it covers digit extraction and counting even and odd digits in a number.

Uploaded by

extrauses337
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views28 pages

QBASIC Programming Loops and Examples

The document provides a series of QBASIC programming examples demonstrating the use of FOR NEXT and WHILE WEND loops. It includes programs for printing even and odd numbers, calculating averages, factorials, and checking for prime numbers, along with explanations and dry runs for each example. Additionally, it covers digit extraction and counting even and odd digits in a number.

Uploaded by

extrauses337
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

BASIC PROGRAMMING PART (2) { COMPUTER SCIENCE TR3 }

●​ FOR NEXT LOOP


●​ WHILE WEND …… LOOP

17/01/2026 . SATURDAY

✅ PROGRAM 1
Print even numbers from 2 to 100

QBASIC Program
CLS
FOR I = 2 TO 100 STEP 2
PRINT I
NEXT I
END

Dry Run (easy)

●​ I starts from 2
●​ STEP 2 → 2, 4, 6, 8 …
●​ Stops at 100

Output (sample)
2
4
6
...
100

Explanation

👉 Loop starts from 2 and increases by 2, so only even numbers are printed.

✅ PROGRAM 2
Accept name & 3 subject marks, print average (20
students)

QBASIC Program
CLS
FOR I = 1 TO 20
INPUT "Enter name "; N$
INPUT "Enter marks "; M1, M2, M3
AVG = (M1 + M2 + M3) / 3
PRINT "Name = "; N$
PRINT "Average = "; AVG
PRINT
NEXT I
END

Dry Run (1 student example)

Input:

Name = Ravi
Marks = 60, 70, 80
Calculation:

AVG = (60 + 70 + 80) / 3


AVG = 70

Output
Name = Ravi
Average = 70

Explanation

👉 Loop runs 20 times, average is calculated for each student.

✅ PROGRAM 3
Print numbers from 100 to 1

QBASIC Program
CLS
FOR I = 100 TO 1 STEP -1
PRINT I
NEXT I
END

Dry Run

●​ I = 100 → printed
●​ I = 99 → printed
●​ …
●​ I = 1 → printed

Output (sample)
100
99
98
...
1

Explanation

👉 STEP -1 makes the loop go backward.

✅ PROGRAM 4
Print HELLO 10 times

QBASIC Program
CLS
FOR I = 1 TO 10
PRINT "HELLO"
NEXT I
END

Dry Run

●​ Loop runs from 1 to 10


●​ Each time prints HELLO

Output
HELLO
HELLO
...
(10 times)

Explanation

👉 FOR loop repeats printing 10 times.

✅ PROGRAM 5
Print HELLO as long as user wants

QBASIC Program
CLS
DO
PRINT "HELLO"
INPUT "Continue (Y/N) "; CH$
LOOP WHILE CH$ = "Y" OR CH$ = "y"
END

Dry Run

User input:

HELLO
Continue → Y
HELLO
Continue → Y
HELLO
Continue → N

Output
HELLO
HELLO
HELLO

Explanation

👉 Program repeats until user enters N.

✅ PROGRAM 6
Accept price & quantity, print net price, repeat till
user wants

QBASIC Program
CLS
DO
INPUT "Enter price "; P
INPUT "Enter quantity "; Q
NP = P * Q
PRINT "Net Price = "; NP
INPUT "Continue (Y/N) "; CH$
LOOP WHILE CH$ = "Y" OR CH$ = "y"
END

Dry Run

Input:

Price = 50
Quantity = 4

Calculation:

NP = 50 * 4 = 200

Output
Net Price = 200

Explanation

👉 Net price is calculated and loop repeats if user says Y.

✅ PROGRAM 7
Accept a number and print factorial

QBASIC Program
CLS
INPUT "Enter number "; N
F=1
FOR I = 1 TO N
F=F*I
NEXT I

PRINT "Factorial = "; F


END

Dry Run (very important)

Input:

N=5

I F F
Calculation

1 1×1 1

2 1×2 2

3 2×3 6

4 6×4 24

5 24 × 5 12
0

Output
Factorial = 120

Explanation

👉 Factorial = multiplication of numbers from 1 to N.


🔹 Question
Write a program to input a number and check whether it is a prime number or
not.

👉 Prime number: A number which has only two factors – 1 and itself.

✅ QBASIC Program
CLS

INPUT "Enter a number "; N

C = 0

FOR I = 1 TO N

IF N MOD I = 0 THEN

C = C + 1

END IF

NEXT I

IF C = 2 THEN

PRINT "Prime Number"

ELSE

PRINT "Not a Prime Number"


END IF

END

🧠 Easy Explanation
●​ MOD checks remainder​

●​ If N MOD I = 0, then I is a factor​

●​ C counts number of factors​

●​ Prime number has exactly 2 factors​

🧪 Dry Run (Easy)


Input:

N = 5

I N MOD Factor C
I ?
1 0 Yes 1

2 1 No 1

3 2 No 1

4 1 No 1

5 0 Yes 2

Final value of C = 2

🖨 Output:
Prime Number

ALL WHILE–WEND
questions

✅ Q1. Display first 10 odd numbers using


WHILE–WEND
Program

CLS

I = 1

C = 1

WHILE C <= 10

PRINT I

I = I + 2

C = C + 1

WEND

END

Dry Run

●​ I = 1 → print​

●​ I = 3 → print​

●​ …​

●​ Stops after 10 odd numbers​

Output

1 3 5 7 9 11 13 15 17 19
Explanation

👉 Adds 2 each time to get odd numbers.

✅ Q2. Display odd numbers from 7 to 50 using


WHILE–WEND

Program

CLS

I = 7

WHILE I <= 50

PRINT I

I = I + 2

WEND

END

Dry Run

●​ Starts at 7​

●​ Keeps adding 2​
●​ Stops before crossing 50​

Output

7 9 11 13 ... 49

Explanation

👉 Prints odd numbers between 7 and 50.

✅ Q3. Print HELLO 10 times using WHILE loop


Program

CLS

I = 1

WHILE I <= 10

PRINT "HELLO"

I = I + 1

WEND

END

Dry Run
●​ I runs from 1 to 10​

●​ Prints HELLO each time​

Output

HELLO

HELLO

...

(10 times)

Explanation

👉 WHILE loop repeats 10 times.

✅ Q4. Display first 20 natural numbers using


WHILE–WEND

Program

CLS

I = 1

WHILE I <= 20

PRINT I

I = I + 1
WEND

END

Dry Run

●​ I starts from 1​

●​ Stops at 20​

Output

1 2 3 4 ... 20

Explanation

👉 Natural numbers start from 1.

✅ Q5. Display 15 natural numbers horizontally


Program

CLS

I = 1

WHILE I <= 15

PRINT I; " ";


I = I + 1

WEND

END

Dry Run

●​ Prints numbers on same line using ;​

Output

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Explanation

👉 Semicolon prints output horizontally.

✅ Q7. Display series 6, 12, 18 … (10 terms)


Program

CLS

I = 6

C = 1

WHILE C <= 10

PRINT I
I = I + 6

C = C + 1

WEND

END

Dry Run

●​ Starts at 6​

●​ Adds 6 each time​

●​ Stops after 10 terms​

Output

6 12 18 24 30 36 42 48 54 60

Explanation

👉 Multiples of 6.

✅ Q8. Print series 1, 8, 2, 7 … (10 terms)


Program

CLS

A = 1
B = 8

C = 1

WHILE C <= 10

PRINT A

PRINT B

A = A + 1

B = B - 1

C = C + 2

WEND

END

Dry Run

●​ Prints one from start, one from end​

●​ Continues till 10 terms​

Output

1 8 2 7 3 6 4 5 5 4

Explanation

👉 One number increases, the other decreases.


✅ Q9. Print series 10, 20, 30 … (10 terms)
Program

CLS

I = 10

C = 1

WHILE C <= 10

PRINT I

I = I + 10

C = C + 1

WEND

END

Dry Run

●​ Starts from 10​

●​ Adds 10 each time​

●​ Stops after 10 terms​

Output
10 20 30 40 50 60 70 80 90 100

Explanation

👉 Series of multiples of 10.


Digit extraction – QBASIC,

✅ Q1. Input a two-digit number, separate the digits


and print them

QBASIC Program

CLS

INPUT "Enter a two-digit number "; N

A = N \ 10

B = N MOD 10

PRINT "First digit = "; A

PRINT "Second digit = "; B

END

Explanation
●​ \ gives quotient (first digit)​

●​ MOD gives remainder (second digit)​

Dry Run

Input: 45

A = 45 \ 10 = 4

B = 45 MOD 10 = 5

Output

First digit = 4

Second digit = 5

✅ Q2. Input a two-digit number and display cube of


each digit

QBASIC Program

CLS

INPUT "Enter a two-digit number "; N

A = N \ 10
B = N MOD 10

PRINT "Cube of first digit = "; A ^ 3

PRINT "Cube of second digit = "; B ^ 3

END

Dry Run

Input: 23

A = 2 → 2³ = 8

B = 3 → 3³ = 27

Output

Cube of first digit = 8

Cube of second digit = 27

✅ Q3. Input a number and count number of EVEN


digits

QBASIC Program

CLS
INPUT "Enter a number "; N

C = 0

WHILE N > 0

D = N MOD 10

IF D MOD 2 = 0 THEN

C = C + 1

END IF

N = N \ 10

WEND

PRINT "Number of even digits = "; C

END

Explanation

●​ Extract last digit​

●​ Check even​

●​ Remove digit​

Dry Run

Input: 2465
Digits → 5, 6, 4, 2​
Even digits → 2, 4, 6

Output

Number of even digits = 3

✅ Q4. Input a number and count number of ODD


digits

QBASIC Program

CLS

INPUT "Enter a number "; N

C = 0

WHILE N > 0

D = N MOD 10

IF D MOD 2 <> 0 THEN

C = C + 1

END IF

N = N \ 10

WEND
PRINT "Number of odd digits = "; C

END

Dry Run

Input: 3487

Odd digits → 7, 3

Output

Number of odd digits = 2

✅ Q5. Check whether a number is Armstrong or not


👉 Armstrong number:​
Sum of cubes of digits = original number​
Example: 153

QBASIC Program

CLS

INPUT "Enter a number "; N

S = 0

T = N
WHILE N > 0

D = N MOD 10

S = S + D ^ 3

N = N \ 10

WEND

IF S = T THEN

PRINT "Armstrong Number"

ELSE

PRINT "Not an Armstrong Number"

END IF

END

Dry Run (VERY IMPORTANT)

Input: 153

Digi Cube
t

3 27
5 125

1 1

S = 27 + 125 + 1 = 153

Since S = T

Output

Armstrong Number

You might also like