For next loop multiplying numbers - vb.net

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

Related

VB keeping track of high and low numbers

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

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

How to Loop through a If condition untill the user enter a required value, in Visual Basic

Console.WriteLine("Please Enter the number")
Dim number As Integer = Console.ReadLine()
If (number=< 40) Then
number = number* 10
ElseIf (number=< 150) Then
number= number* 15
Else
number= number* 26
End If
Console.WriteLine(number)
Dim total As Integer
Dim vALUE As Integer
Console.WriteLine("Please, type 1 for x . Type 2 for y. Type 3 z")
vALUE = Console.ReadLine()
If vALUE = 1 Then
Console.WriteLine("x")
total = number* (106 / 100)
ElseIf vALUE = 2
Console.WriteLine("y")
total = number* (112 / 100)
ElseIf uSERvALUE = 3
Console.WriteLine("z")
total = number* (116 / 100)
Else
Console.WriteLine("Sorry please re-enter the value")
vALUE = Nothing
End If
End While
Please tell me how to repeat the if condition. In the else line I have displayed to the user to re-enter the value. Therefore I need to repeat the if condition until the value is 1 or 2 or 3.Please explain how to do I'm a newbie.
You can use infinite While-loop ,and put "Exit While" on the end of conditional statement you agreed , so there are few ways out from infinite loop.
For example , on your code:
Console.WriteLine("Please Enter the number")
Dim number As Integer = Console.ReadLine()
If (number=< 40) Then
number = number* 10
ElseIf (number=< 150) Then
number= number* 15
Else
number= number* 26
End If
Console.WriteLine(number)
Dim total As Integer
Dim vALUE As Integer
'infinite loop until user input 1,2 or 3
While True
Console.WriteLine("Please, type 1 for x . Type 2 for y. Type 3 z")
vALUE = Console.ReadLine()
If vALUE = 1 Then
Console.WriteLine("x")
total = number* (106 / 100)
Exit While 'condition matched , break from While
ElseIf vALUE = 2
Console.WriteLine("y")
total = number* (112 / 100)
Exit While 'condition matched , break from While
ElseIf uSERvALUE = 3
Console.WriteLine("z")
total = number* (116 / 100)
Exit While 'condition matched , break from While
Else
Console.WriteLine("Sorry please re-enter the value")
vALUE = Nothing
End If
End While
'do further more you need

(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

Max Value in VB?

Here is the code I'm working on for a project.
What I can't figure out is the max value and how to get it?
Even if I change it, it seems not to affect the outcome of the counter?
Can anyone lead me in the right direction on what to do for this?
Thanks!
Module
Module1
Dim counter As Integer
Const Max_Value As Double = 22
Sub Main()
Console.WriteLine("Are you ready to see which letter is 22? Press Enter")
Console.ReadLine()
For counter As Integer = 0 To Max_Value Step 1
 
Console.Write("a")
Console.WriteLine("0")
counter = counter + 1
Console.Write("b")
Console.WriteLine("1")
counter = counter + 1
Console.Write("c")
Console.WriteLine("2")
counter = counter + 1
Console.Write("d")
Console.WriteLine("3")
counter = counter + 1
Console.Write("e")
Console.WriteLine("4")
counter = counter + 1
Console.Write("f")
Console.WriteLine("5")
counter = counter + 1
Console.Write("g")
Console.WriteLine("6")
counter = counter + 1
Console.Write("h")
Console.WriteLine("7")
counter = counter + 1
Console.Write("i")
Console.WriteLine("8")
counter = counter + 1
Console.Write("j")
Console.WriteLine("9")
counter = counter + 1
Console.Write("k")
Console.WriteLine("10")
counter = counter + 1
Console.Write("l")
Console.WriteLine("11")
counter = counter + 1
Console.Write("m")
Console.WriteLine("12")
counter = counter + 1
Console.Write("n")
Console.WriteLine("13")
counter = counter + 1
Console.Write("o")
Console.WriteLine("14")
counter = counter + 1
Console.Write("p")
Console.WriteLine("15")
counter = counter + 1
Console.Write("q")
Console.WriteLine("16")
counter = counter + 1
Console.Write("r")
Console.WriteLine("17")
counter = counter + 1
Console.Write("s")
Console.WriteLine("18")
counter = counter + 1
Console.Write("t")
Console.WriteLine("19")
counter = counter + 1
Console.Write("u")
Console.WriteLine("20")
counter = counter + 1
Console.Write("v")
Console.WriteLine("21")
counter = counter + 1
Console.Write("w")
Console.WriteLine("22")
Console.ReadLine()
Next
Console.WriteLine()
Console.WriteLine("Summary of the Count: {0} So we've counted to 22{0} The Winning letter is W{0} Which is great because that's the letter of my first name{0} W also stands for War Eagle!", _
Environment.NewLine)
  
Console.WriteLine("Press Enter to Exit")
Console.ReadLine()
End Sub
End
Module
I think what you are trying to accomplish is something like this:
Module Module1
Dim counter As Integer
Const Max_Value As Integer = 22
Sub Main()
Console.WriteLine("Are you ready to see which letter is 22? Press Enter")
Console.ReadLine()
For counter As Integer = 0 To Max_Value Step 1
Select Case counter
Case 0
Console.Write("a")
Case 1
Console.Write("b")
Case 2
Console.Write("c")
Case 3
Console.Write("d")
Case 4
Console.Write("e")
Case 5
Console.Write("f")
Case 6
Console.Write("g")
Case 7
Console.Write("h")
Case 8
Console.Write("i")
Case 9
Console.Write("j")
Case 10
Console.Write("k")
Case 11
Console.Write("l")
Case 12
Console.Write("m")
Case 13
Console.Write("n")
Case 14
Console.Write("o")
Case 15
Console.Write("p")
Case 16
Console.Write("q")
Case 17
Console.Write("r")
Case 18
Console.Write("s")
Case 19
Console.Write("t")
Case 20
Console.Write("u")
Case 21
Console.Write("v")
Case 22
Console.Write("w")
End Select
Console.WriteLine(counter)
Console.ReadLine()
Next
Console.WriteLine()
Console.WriteLine("Summary of the Count: {0} So we've counted to 22{0} The Winning letter is W{0} Which is great because that's the letter of my first name{0} W also stands for War Eagle!", Environment.NewLine)
Console.WriteLine("Press Enter to Exit")
Console.ReadLine()
End Sub
End Module
In your version of the code, it outputs all of the letters each time it loops. In my version of the code, it only outputs one letter each time it loops. The Select Case statement is basically a simpler way of writing a bunch of separate If statements.
However, it's silly to write a big Select Case like that. The best way to implement a loop is to have it iterate over some sort of indexed data-structure. In this case, all you need is a list of letters. The simplest way to do that is to just store all of the letters in a single string, like this:
Dim letters As String = "abcdefghijklmnopqrstuvw"
For counter As Integer = 0 To Max_Value Step 1
Console.Write(letters(counter))
Console.WriteLine(counter)
Console.ReadLine()
Next
Create seperate variable to be used in for loop. What is happening is that you are looping with variable counter and then incrementing it inside loop. This will always loop 22 times.
So declare seperate variable and loop on it.
Dim intC as integer
For intC = 0 to Max_Value step 1
counter = counter + 1
Next
You are declaring counter on Module level and as a private variable for your iteration.
Every time your for loop goes to the next step, the private counter is overwritten with a new value. You should rename it something else
For c As Integer = 0 To Max_Value Step 1
counter = counter + 1
Next
or
For c As Integer = 0 To Max_Value Step 1
counter = c
Next
you probably want this:
For c as Integer = 0 To Max_Value ' Step 1 is default so you can skip that
Console.Write(Convert.ToChar(c + 97)) ' 97 = a
Console.WriteLine(c)
Next
Update: This has not much to do with your original code but is an alternative approach
Dim maxvalue As Integer = 22
Dim counter As Integer = 0
For Each c As Char In "abcdefghijklmnopqrstuvw".ToCharArray()
counter += 1
If counter = maxvalue Then
Dim ordinal As String = "th"
Select Case counter
Case 1 : ordinal = "st"
Case 2 : ordinal = "nd"
Case 3 : ordinal = "rd"
End Select
Console.WriteLine("{0} is the {1}{2} letter in the alphabet", c, maxvalue, ordinal)
Exit For
End If
Next