0% found this document useful (0 votes)
11 views31 pages

Programming Exercises and Solutions

This document contains exercises and solutions for chapter 6 of a programming textbook. It includes: 1. Multiple choice and true/false questions about loops and conditions in programming. 2. Code snippets demonstrating the use of loops, conditions, arrays, and printing output to draw shapes like triangles and star patterns. 3. Two longer code examples that use loops and input/output to find prime factors of a number and print a Celsius to Fahrenheit conversion table.

Uploaded by

Mustafa Radaideh
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)
11 views31 pages

Programming Exercises and Solutions

This document contains exercises and solutions for chapter 6 of a programming textbook. It includes: 1. Multiple choice and true/false questions about loops and conditions in programming. 2. Code snippets demonstrating the use of loops, conditions, arrays, and printing output to draw shapes like triangles and star patterns. 3. Two longer code examples that use loops and input/output to find prime factors of a number and print a Celsius to Fahrenheit conversion table.

Uploaded by

Mustafa Radaideh
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

Chapter six selected exercises

with solutions
6.1
1. 17
2. 0 1000
1 1100
2
3. You are a super programmer!
7. The value of q keeps growing until the
program crashes when the value exceeds
the upper limit for a variable of the type
Single.
8. Program never stops. The line n = 1
should appear before the Do loop.
11. While num >= 7
12. While nom <> "Bob"
13. Until response <> "Y"
14. Until total <> 10
15. Until nom = ""
16. While balance < 100
17. Until (a <= 1) Or (a >= 3)
18. While (ans <> "") And (n <> 0)
19. While n = 0
20. Until (ans <> "Y") Or (n >= 7)
Private Sub cmdDisplayConvTable_Click()
Dim celsius As Single
[Link]
[Link] "Celsius"; Tab(10); "Fahrenheit"
celsius = -40
Do While celsius <= 40
[Link] celsius; Tab(10); (9 / 5) * celsius + 32
celsius = celsius + 5
Loop
End Sub
Private Sub cmdFindPrimeFactors_Click()
Dim n As Integer, f As Integer
'Prime factorization
[Link]
n = Val(InputBox("Enter an integer greater than 1:"))
[Link] "The prime factors of"; n; "are:"
f=2
Do While n > 1
If (n Mod f) <> 0 Then 'Or If Int(n/f) <> n/f Then
f=f+1
Else
[Link] f;
n=n/f
End If
Loop
End Sub
6.2
1. 13 2. Roxie 3. pie
Velma cake
melon
4. Chicago 8.8
New York 20.1
6.3
1. Pass # 1
Pass # 2
Pass # 3
Pass # 4
2. 6 8 10 12
3. 2 4 6 8 Who do we appreciate?
8. Average = 90
17. Private Sub cmdDisplay_Click()
Dim num As Integer
For num = 1 To 10 Step 2
[Link] num
Next num
End Sub
18. Private Sub cmdDisplay_Click()
Dim num As Integer
For num = 1 To 4
[Link] "hello"
Next num
End Sub
19.
Private Sub cmdDisplay_Click()
Dim i As Integer
'Display a row of 10 stars
[Link]
For i = 1 To 10
[Link] "*";
Next i
End Sub

20.
Private Sub cmdDisplay_Click()
Dim i As Integer, stars As Integer
'Display a row of stars
[Link]
stars = Val(InputBox("Row length (1-20) : "))
For i = 1 To stars
[Link] "*";
Next i
End Sub
21.

Private Sub cmdDisplay_Click()


Dim i As Integer, j As Integer
'Display 10 x 10 array of stars
[Link]
For i = 1 To 10
For j = 1 To 10
[Link] "*";
Next j
[Link]
Next i
End Sub
35.
Private Sub cmdDraw_Click()
Dim stars As Integer, i As Integer
[Link]
[Link] = "Courier New"
stars = Val(InputBox("Number of stars?"))

For i = 1 To stars
[Link] "*";
Next i
[Link]

For i = 1 To stars - 2
[Link] "*";
For j = 1 To stars - 2
[Link] " ";
Next j
[Link] "*"
Next i

For i = 1 To stars
[Link] "*";
Next i

End Sub
36.
Private Sub cmdDraw_Click()
Dim stars As Integer, topSize As Integer
'Draw a triangle
[Link]
[Link] = "Courier New"
topSize = Val(InputBox("Enter an odd number:"))
For stars = topSize To 1 Step -2
For i = 1 To stars
[Link] "*";
Next i
[Link]
Next stars
End Sub

You might also like