Write a statement or a set of statements to accomplish each of the following:
>Sum the odd integers between 1 and 99 using a For/Next structure. Assume that variables sum and count have been declared explicitly as Integer.
>Print the number from 20 to 1 on the form using a Do Until loop and Integer counter variable x. Assume that the variable x is initialized to 20.
>Print the Integers from 1 to 20 using a Do/Loop While structure and the counter variable x. Assume that x has been declared as an integer but not initialized.
Module Module1
Sub Main()
'Sum the odd integers between 1 and 99 using a For/Next structure. Assume that variables sum and count have been declared explicitly as Integer.
Dim sum As Integer = 0
Dim count As Integer = 0
For i As Integer = 1 To 99
If i Mod 2 <> 0 Then
count += 1
sum += i
End If
Next
Console.WriteLine("Sum = {0}", sum)
Console.WriteLine("Count = {0}", count)
Console.WriteLine()
Console.WriteLine("The number from 20 to 1 on the form using a Do Until loop:")
'Print the number from 20 to 1 on the form using a Do Until loop and Integer counter variable x. Assume that the variable x is initialized to 20.
Dim x As Integer = 20
Do
Console.WriteLine("{0}", x)
x -= 1
Loop Until x < 1
'Print the Integers from 1 to 20 using a Do/Loop While structure and the counter variable x. Assume that x has been declared as an integer but not initialized.
Console.WriteLine()
Console.WriteLine("The number from 1 to 20 on the form using a Do/Loop While:")
x = 1
Do
Console.WriteLine("{0}", x)
x += 1
Loop While x <= 20
Console.ReadLine()
End Sub
End Module
Comments
Leave a comment