VB keeping track of high and low numbers - vb.net

After running through the following program, lownum stays 0. It makes complete sense if you do not enter any negative numbers. However, what are my options to get the lowest number if all positive numbers are entered.
Sub Main()
Declaring variables
Dim number1 As Integer = 0
Dim number2 As Integer = 0
Dim lownum As Integer
Dim highnum As Integer
'For statement to run loop 10 times
For counter As Integer = 1 To 10
'Prompting user to enter two numbers
Console.Write("Enter the first number: ")
number1 = Console.ReadLine
Console.Write("Enter the second number: ")
number2 = Console.ReadLine
'If statements to determine and keep track of highest and lowest number
If number1 > number2 Then
Console.WriteLine("Number 1 is larger " & number1)
ElseIf number2 > number1 Then
Console.WriteLine("Number 2 is larger " & number2)
Else
Console.WriteLine("The two numbers are equal: " & number1 & " " & number2)
End If
If number1 > highnum Then
highnum = number1
End If
If number1 < lownum Then
lownum = number1
End If
If number2 > highnum Then
highnum = number2
End If
If number2 < lownum Then
lownum = number2
End If
Next
'Displaying highest and lowest numbers
Console.WriteLine("The highest number entered was " & highnum)
Console.WriteLine("The lowest number entered was " & lownum)
'Prompting user for input to continue
Console.WriteLine("Press any key to continue: ")
Console.ReadKey()
End Sub

Just check your counter variable. If it is 1, then set low and high to your values appropriately. If it is greater than 1, then do the comparisons:
For counter As Integer = 1 To 10
' ... other code ...
If counter = 1 Then
lownum = Math.Min(number1, number2)
highnum = Math.Max(number1, number2)
Else
lownum = Math.Min(lownum, Math.Min(number1, number2))
highnum = Math.Max(highnum, Math.Max(number1, number2))
End If
Next

You testing for values being less or greater then your two starting variables of 0.
If you don't give the min/max values and don't enter values < 0 or > 0 then the values of Min/Max are NEVER set.
So you want to set these to the max allowable and min allowable values.
Change you variable declares to this with initialization of the min/max and your code should work just fine.
Dim number1 As Integer = 0
Dim number2 As Integer = 0
Dim lownum As Integer = Integer.MaxValue
Dim highnum As Integer = Integer.MinValue

Related

Visual basics help-Variables in for loop

I'm trying to get the integers of "num3"(input from the user) and squaring each character and eventually see if its a "happy number"=1 or a "unhappy number" includes 4
For example, 19 is happy number 1^2+9^2=82 , 8^2+2^2=68,6^2+8^2=100, 1^2+0+0=1
Sub Main()
Dim number, num2, total As Integer
Dim num3 As String
Console.Write("pick a number:")
number = Console.ReadLine()
num2 = 0
num3 = Convert.ToString(number)
Do Until total = 1 Or num2 = 4
For Each num3 In num3.Substring(0, num3.Length)
For counter = 1 To num3.Length And num3 = num3.Substring(0, num3.Length)
num2 = num3 ^ 2
total = total + num2
Console.WriteLine(total)
Next
num3 = total
Next
Loop
Console.ReadLine()
If total = 1 Then
Console.WriteLine("happy number")
ElseIf num2 = 4 Then
Console.WriteLine(number & "is a unhappy number")
Console.ReadLine()
End If
End Sub
End Module
however i'm stuck whether "num3" replace itself from the for loop
Here, I believe this should work:
Dim InputNum As Integer = 45 'Your input number
Dim TempNum As Integer = InputNum 'Here we hold number temporarily during steps 19>82....
Do
Dim SumTotal As Double = 0 'sum of numbers squares
For Each number As Char In TempNum.ToString
SumTotal += Integer.Parse(number) ^ 2
Next
TempNum = CInt(SumTotal)
Debug.Print(TempNum.ToString)
'If result is 4 it will loop forever
If SumTotal = 4 Then
Console.WriteLine(InputNum & " is an unhappy number")
Exit Do
ElseIf SumTotal = 1 Then
Console.WriteLine(InputNum & " is a happy number")
Exit Do
End If
Loop

Randomly find numbers from 1-12 without repeat [duplicate]

This question already has an answer here:
Pick unique Random numbers
(1 answer)
Closed 5 years ago.
I have three variable integers, and I have the following code that randomizes their value:
Randomize()
number = Int(Rnd() * 12) + 1
AssignImagesToSquares()
number2 = Int(Rnd() * 12) + 1
AssignImagesToSquares()
number3 = Int(Rnd() * 12) + 1
AssignImagesToSquares()
And AssignImagesToSquares is a Private Sub where I use them.
However, the problem that I am facing is that numbers can be repeated. I could not figure out how to do it, but in psuedocode,
'Randomize the integer "number"
'Randomize the integer "number2" where number2 <> number
'Randomize the integer "number3" where number3 <> number2 <> number.
I thought of maybe using a loop to repeat the process until a match is found but how exactly can that be done?
As a simple solution you could just use a Do..Loop until the numbers do not match, for example
Randomize()
number = Int(Rnd() * 12) + 1
AssignImagesToSquares()
Do
number2 = Int(Rnd() * 12) + 1
If number2 <> number Then
AssignImagesToSquares()
Exit Do
End If
Loop
Do
number3 = Int(Rnd() * 12) + 1
If number3 <> number AndAlso number3 <> number2 Then
AssignImagesToSquares()
Exit Do
End If
Loop
Yes you could use loops, but alternatively given your situation you could store your values in array, and take values from this array, and as soon as you pick a value from array you remove it. Then you could use it again. Simple code provided (of course it would be better wrapped in a function):
Dim number1, number2, number3 as Integer
Dim numbers = New Integer() {1,2,3,4,5,6,7,8,9,10,11,12}
Dim indx As Integer = Int(Rnd() * numbers.Length)
number1=numbers(indx)
Console.WriteLine(number1)
System.Array.Clear(numbers, indx, 1)
indx=Int(Rnd() * numbers.Length) 'wrap in function
number2=numbers(indx) '
Console.WriteLine(number2) 'AssignImagesToSquares()
System.Array.Clear(numbers, indx, 1) '
MAYBE a little overkill ;) , but you can extend this for any number of numbers wanted:
Dim temp As New ConcurrentDictionary(Of Integer, Integer)
Dim count_actual As Integer = 0
Dim count_wanted As Integer = 3
Do
Dim number = Int(Rnd() * 12) + 1 'or whatever random function
If temp.TryAdd(number, count_actual) Then
count_actual += 1
End If
Loop While count_actual < count_wanted
Dim yourNumbers = temp.OrderBy(Function(v) v.Value).Select(Of Integer)(Function(v) v.Key).ToArray()
Now there are your wanted random different numbers in the yourNumbers array. Just use them.

(Visual Basic) Sum of integers through 2 numbers

So im pretty close but I continue to get the wrong values. The user is suppose to enter a positive integer and its suppose to add all the integers in between. So if the user enters 5 it should equal 15, 10 would equal 55, etc. But I get 5 = 25, 10, 100.
Changed to decimal to see if that had anything instead of integer and still did nothing. I saw a few things to set decCount to = 1. Did that and the number was closer but still not there.
Dim decSum As Decimal = 0
Dim decNumber As Decimal = 0
Dim decCount As Decimal = 0
Dim strUserInput As String
strUserInput = InputBox("Enter a positive integer value.", "Input Needed", 0)
If Decimal.TryParse(strUserInput, decNumber) And (decNumber >= 0) Then
Do While decCount < decNumber
decSum = decSum + decNumber
decCount = decCount + 1
Loop
Else
MessageBox.Show("Enter a positive numeric value")
End If
MsgBox("The sum of the numbers 1 through " & decNumber & " is " & decSum)
You are trying to calculate a factorial of a given input, but in your loop you are adding the same number repeatedly (effectively, you are multiplying the number by itself instead of finding the factorial).
Change this line:
decSum = decSum + decNumber
to this:
decSum = decSum + decCount

For next loop multiplying numbers

I am attempting to create a calculator where a user inputs a number (1-20) and it gives the user the option to get the sum or product from 1 to the number they entered. ie the sum of 5 would be 15 while the product would be 120. I am using select case and a for next loop. I have managed to get the sum portion of the calculator working correctly and thought I could use the same principle for the product portion but I am having no such luck. Any help or a gentle nudge in the right direction would greatly appreciated.
Cheers.
Code:
Dim intsum As Integer
Dim intnum As Integer
Dim intproduct As Integer
If IsNumeric(txtinput.Text) Then
intnum = CInt(txtinput.Text)
Else
MessageBox.Show("Please enter a numeric value", "Input error")
txtinput.Clear()
txtinput.Focus()
End If
If intnum >= 1 AndAlso intnum <= 20 Then
Select Case True
Case btnproduct.Checked
For P = 1 To intnum Step 1
intproduct = intproduct * P
Next
Case btnsum.Checked
For S = 1 To intnum Step 1
intsum = intsum + S
Next
End Select
Initialise intproduct to = 1 as at the moment you are multiplying your value by 0 so it will always show the final result as 0.
You don't need to check the value of intnum if the value is not numeric.
Dim intsum As Integer
Dim intnum As Integer
Dim intproduct As Integer
If IsNumeric(txtinput.Text) Then
intnum = CInt(txtinput.Text)
If intnum >= 1 AndAlso intnum <= 20 Then
Select Case True
Case btnproduct.Checked
For P = 1 To intnum Step 1
intproduct = intproduct * P
Next
Case btnsum.Checked
For S = 1 To intnum Step 1
intsum = intsum + S
Next
End Select
End If
Else
MessageBox.Show("Please enter a numeric value", "Input error")
txtinput.Clear()
txtinput.Focus()
End If

How to randomize a math function/operator in visual basic (console)

This is my code so far
Module Module1
Sub Main()
Randomize()
Dim number1 As Integer
Dim number2 As Integer
Dim answer As Integer
Dim userAnswer As Integer
Dim name As String
Console.WriteLine("Hello! Welcome to your Maths Quiz! Please enter your name >")
name = Console.ReadLine
Console.WriteLine("Nice to meet you " + name + ". Lets start the quiz")
Randomize()
number1 = Rnd() * 11 + 1
number2 = Rnd() * 11 + 1
answer = number1 + number2
Try
For i As Integer = 0 To 10
Console.WriteLine("What is " & number1 & " + " & number2 & " = ?")
userAnswer = Console.ReadLine
If userAnswer = answer Then
Console.WriteLine("Correct!")
Else
Console.WriteLine("Incorrect, the answer was " & answer)
End If
Next
Catch ex As InvalidCastException
Console.WriteLine("Oops you have typed in a number, please start over")
End Try
Console.ReadKey()
End Sub
End Module
I need to create a random function to take place of the "+" sign and i have tried many ways but the output comes up weird, i was wondering if you can help, Thanks
If you are going for + - * / then I would do this, if only + - and could go with IIF statments
Create a new random variable operator
op = Int(Rnd() * 4) '0+ 1- 2* 3/
Calculate the anwser with a function
answer= calc(number1, number2, op)
Function calc(n1, n2, op)
If op = 0 Then calc = n1 + n2
If op = 1 Then calc = n1 - n2
If op = 2 Then calc = n1 * n2
If op = 3 Then calc = n1 / n2
End Function
And 1 more function to get the operator sign
Console.WriteLine("What is " & number1 & s_op(op) & number2 & " = ?")
Function s_op(op)
If op = 0 Then s_op = "+"
If op = 1 Then s_op = "-"
If op = 2 Then s_op = "*"
If op = 3 Then s_op = "/"
End Function