a.abdullah - 9 months ago 53

Vb.net Question

I want to write a programm, that first generates three random integers and then prints out the square and cube of them.

I already have the calculation and the output, but I don´t know how to generate the random numbers.

`Dim NumOfIntegers() As Integer = {1, 2, 3, 4, 8}`

Dim x, y As Integer

For Each num As Integer In NumOfIntegers

x = num ^ 2

y = num ^ 3

MessageBox.Show("Square Of " & num & " = " & x & vbCrLf & "Cube Of " & num & " = " & y)

Next

Answer Source

This Code fills a Integer List with 3 different random numbers between 1 and 100

```
Dim NumOfIntegers As New List(Of Integer)
While NumOfIntegers.Count < 3
Dim i As Integer = CInt(Math.Ceiling(Rnd() * 100)) + 1
If Not NumOfIntegers.Contains(i) Then NumOfIntegers.Add(i)
End While
NumOfIntegers.Sort()
Dim x, y As Integer
For Each num As Integer In NumOfIntegers
x = num ^ 2
y = num ^ 3
MessageBox.Show("Square Of " & num & " = " & x & vbCrLf & "Cube Of " & num & " = " & y)
Next
```

Code if you only want to use a for loop:

(It´s untested code and within the 3 Integers might be duplicates)

```
Dim NumOfIntegers() As New Integer = {(CInt(Math.Ceiling(Rnd() * 100)) + 1), (CInt(Math.Ceiling(Rnd() * 100)) + 1), (CInt(Math.Ceiling(Rnd() * 100)) + 1)}
Dim x, y As Integer
For Each num As Integer In NumOfIntegers
x = num ^ 2
y = num ^ 3
MessageBox.Show("Square Of " & num & " = " & x & vbCrLf & "Cube Of " & num & " = " & y)
Next
```