0% found this document useful (0 votes)
69 views5 pages

Count-Controlled Loops: Exercises

The document describes 7 exercises involving count-controlled loops in programming. 1. Write programs to count from 1 to a given number, count down from a given number to 1, and count by a given interval between numbers. 2. Write programs to input 10 numbers, find the largest number, and find the smallest number. 3. Write a program to repeatedly prompt for a number between 1 and 5 until a valid number is entered. 4. Write a program to count from a given starting number to a given ending number.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views5 pages

Count-Controlled Loops: Exercises

The document describes 7 exercises involving count-controlled loops in programming. 1. Write programs to count from 1 to a given number, count down from a given number to 1, and count by a given interval between numbers. 2. Write programs to input 10 numbers, find the largest number, and find the smallest number. 3. Write a program to repeatedly prompt for a number between 1 and 5 until a valid number is entered. 4. Write a program to count from a given starting number to a given ending number.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Exercises

Count-controlled loops

1. Write a program to take as input a positive integer. The program is to output integers counting from 1 to the number input. For
example, an input of 5 will output: 1 2 3 4 5
2. Amend your program from (1) to count down from the number input to 1. For example, an input of 5 will output: 5 4 3 2 1
3. Amend your program from (1) to take as input a second integer to be used as counting interval. For example, the input of 20, 4
will output: 1 5 9 13 17
4. Write a program to take as input ten numbers and output the largest number.
5. Amend your program from (5) to output the smallest number.

Pre-condition and post-condition loops

6. Write a program that asks the user for an integer between 1 and 5 (inclusive). The program is to check that the input is within
the range and if not, prompt the user for a valid integer until the input is within the range.
7. Write a program to take as input two positive integers. The program is to output integers counting from the first integer to the
second integer. For example, the input 3, 7 will output: 3 4 5 6 7
Public Class frmLoopsExercises

Private Sub BtnQuestion1_Click(sender As System.Object, e As System.EventArgs) Handles BtnQuestion1.Click


'1. Write a program to take as input a positive integer. The program is to output integers counting from 1 to the
'number input. For example, an input of 5 will output: 1 2 3 4 5
Dim Number As Integer = InputBox("Enter Number")
Dim i As Integer

Do
Number = InputBox("Enter Number")
Loop Until Number > 0

For i = 1 To Number
ListBox1.Items.Add(i)
Next i
End Sub

Private Sub BtnQuestion2_Click(sender As System.Object, e As System.EventArgs) Handles BtnQuestion2.Click


'2. Amend your program from (1) to count down from the number input to 1. For example, an input of 5 will output: 5
' 4 3 2 1
Dim Number As Integer = InputBox("Enter Number")
Dim i As Integer

For i = Number To 1 Step -1


ListBox1.Items.Add(i)
Next i
End Sub

Private Sub BtnQuestion3_Click(sender As System.Object, e As System.EventArgs) Handles BtnQuestion3.Click


'3. Amend your program from (1) to take as input a second integer to be used as counting interval. For example,
'the input of 20, 4 will output: 1 5 9 13 17
Dim countingInterval As Integer = InputBox("Enter counting Interval")
Dim Number As Integer = InputBox("Enter Number")
Dim i As Integer

For i = 1 To Number Step countingInterval


ListBox1.Items.Add(i)
Next i
End Sub
Private Sub BtnQuestion4_Click(sender As System.Object, e As System.EventArgs) Handles BtnQuestion4.Click
'4. Take 10 numbers as input and output the largest number.
Dim BiggestSoFar, nextNumber As Integer
Dim Counter As Integer

BiggestSoFar = InputBox("Please enter first Number")


Counter = 1

Do
nextNumber = InputBox("Please enter next Number")
Counter = Counter + 1
If nextNumber > BiggestSoFar Then
BiggestSoFar = nextNumber
End If
Loop Until Counter = 10

MessageBox.Show("The biggest so far is: " & BiggestSoFar)


End Sub

Private Sub BtnQuestion5_Click(sender As System.Object, e As System.EventArgs) Handles BtnQuestion5.Click


'5. Take 10 numbers as input and output the smallest number.
Dim smallestSoFar, nextNumber As Integer
Dim Counter As Integer

smallestSoFar = InputBox("Please enter first Number")


Counter = 1

Do
nextNumber = InputBox("Please enter next Number")
Counter = Counter + 1
If nextNumber < smallestSoFar Then
smallestSoFar = nextNumber
End If
Loop Until Counter = 10

MessageBox.Show("The smallest so far is: " & smallestSoFar)


End Sub
Private Sub BtnQuestion6_Click(sender As System.Object, e As System.EventArgs) Handles BtnQuestion6.Click
'6. Write a program that asks the user for an integer between 1 and 5 (inclusive). The program is to check that the
'input is within the range and if not, prompt the user for a valid integer until the input is within the range.
Dim Number As Integer

Number = InputBox("Enter Number")

Do
Number = InputBox("Enter Number")
Loop Until Number >= 1 And Number <= 5
End Sub

Private Sub BtnQuestion7_Click(sender As System.Object, e As System.EventArgs) Handles BtnQuestion7.Click


'7. Write a program to take as input two positive integers. The program is to output integers counting from the
'first integer to the second integer. For example, the input 3, 7 will output: 3 4 5 6 7
Dim startNumber As Integer = Val(txtStartNumber.Text)
Dim endNumber As Integer = Val(txtEndNumber.Text)
Dim i As Integer

For i = startNumber To endNumber


ListBox1.Items.Add(i)
Next i
End Sub
End Class

You might also like