Sum of the first 100 even and odd number? - vb.net

I need some help getting this code to work. I need the code to calculate the first 100 even and odd numbers. The code below shows the even portion, but I assume they both will work the same way, just with different numbers. I know that the first 100 even numbers are 2 to 200 or 0 to 200?, and the first 100 odd numbers are 1 to 199. (I also need to use while loops, would I use a separate while loop to determine how the code calculate the sum?)
here is the code.
Dim sum, count As Integer 'the sum and count are changing and they must be a whole number.
Const num As Integer = 2
'now we must deveolp a while loop/equation that adds every number lower or equal to 100 together.
While count Mod 2 <= 200 'while the count is smaller or equal to 100,
count =
sum += count 'The sum will be add or equaled to the count
count += 1 'The count will be added to equaled to 1
End While
Console.WriteLine("The Sum of the first 100 even numbers is: {0}!", sum) 'This is the text the user sees, which tells them that the sum of the first 100 numbers will be 5050.
Console.ReadLine()
End Sub
As usual the math portion is giving me trouble. I feel like I have the right idea, but I cant execute it properly.

Dim sumEven, sumOdd As Integer
Dim even = 2
Dim odd = 1
While even <= 200 AndAlso odd <= 200
sumEven += even
sumOdd += odd
even += 2
odd += 2
End While
Console.WriteLine("The Sum of the first 100 even numbers is: {0}!", sumEven)
Console.WriteLine("The Sum of the first 100 odd numbers is: {0}!", sumOdd)

Related

Sum of input number with do loops until

Quick problem on my homework. I have to make a version of this code will "Do Until Loop". The first part regarding the "please enter a positive number" works, but not the second part, that calculates the sum.
Sub Main()
Dim sumOdd, oddinput As Integer
Dim odd = 1
Console.Write("Please Enter a Positive Odd Number!: ")
oddinput = CInt(Console.ReadLine())
Do Until oddinput < 0 Or oddinput Mod 2 = 0
Console.Write("Please Enter a Positive Odd Number!: ")
oddinput = CInt(Console.ReadLine())
Loop
'The do until loop below is giving me the issue
Do Until odd <= oddinput
sumOdd += odd
odd += 2
Loop
Console.WriteLine("The Sum of all the odd numbers up to {0} is {1}!", oddinput, sumOdd)
Console.ReadLine()
End Sub
End Module
The first part actually does not do what you want it to.
oddinput < 0
will accept any number smaller than zero
oddinput Mod 2 = 0
will accept any even number, not odd.
But what you want is to ask the user to enter a number until it is positive and odd:
Do Until oddinput > 0 And oddinput Mod 2 = 1
Console.Write("Please Enter a Positive Odd Number!: ")
oddinput = CInt(Console.ReadLine())
Loop
For the second part you want it to add odd numbers until odd is bigger than oddinput, then stop.
Do Until odd > oddinput
sumOdd += odd
odd += 2
Loop
In your last assignment you had to use While loop so you could say: while odd is smaller or equal the final number. Here you do basically the opposite: Do until odd is bigger than the final number.
Also do not use >= as the loop would stop when odd is the same as the final number, but you want this one still to be added.
Try:
Do Until oddinput < 0 Or oddinput Mod 2 = 1
You were asking for an odd number, but the condition only accepted an even one.
And:
Do Until odd > oddinput
Previously, with odd = 1 and the condition odd <= oddinput, for any oddinput bigger than 1, the loop would have just exited.
You should initialize sumOdd at the begining.
sumOdd=0
And odd should start with 2 and not 1
Dim odd = 2
Replace also this line:
Do Until odd > oddinput
That will work. After that you need to verify the validation because it's validating for example the -2.

How Do I Count When Numbers Do Not Show Up

I have numbers from 1 to 25, four numbers will show up daily. I need to put a +1 on each of the four numbers and need to put a -1 on each of the 21 numbers didn't show up.
The four numbers that come up daily will be inputted in four different text boxes. The count being positive or negative needs to go on 25 separate text boxes labeled 1 thru 25.
I have tried "if textbox <> number, then count -= 1" but I get a count of -4 because it doesn't see the number in any of the four text boxes.
I only need a daily count not a textbox count. Sorry I don't have any code started and would greatly appreciate if someone can point me in the right direction. I'm doing this on Visual Studio 2012.
Thank you all for responding. Here is some code I've started but the count is not correct. My four input text boxes are in GroupBox2. Four numbers from 1 to 25 will draw daily like a lottery. The four numbers drawn will have a value of +1 each all others -1. I need to find the age of each number 1 thru 25. If a number has a +3 then that means that number has drawn 3 consecutive days. If a number has a -15 then that means that number has not drawn for the past 15 days.
Dim tb As New TextBox
Dim ctrl As Control
Dim Counter As Integer
For Each ctrl In GroupBox2.Controls
tb = ctrl
If tb.Text = 1 Then
Counter += 1
ElseIf tb.Text <> 1 Then
Counter -= 1
TextBox464.Text = Counter
End If
If tb.Text = 2 Then
Counter += 1
ElseIf tb.Text <> 2 Then
Counter -= 1
TextBox463.Text = Counter
End If
If tb.Text = 3 Then
Counter += 1
ElseIf tb.Text <> 3 Then
Counter -= 1
TextBox462.Text = Counter
End If
If tb.Text = 4 Then
Counter += 1
ElseIf tb.Text <> 4 Then
Counter -= 1
TextBox461.Text = Counter
End If
Next
We will need more information about how your going to approach it to be able to help you further, but as for your problem with this If Textbox <> number Then count -= 1 you can use something like this since your only going to be having numbers on the textboxes If Cint(Textbox.Text) <> number then count -= 1 since your using just Textbox its attempting to evaluate it as a control and not the property that your looking for, you need to read from its .Text Property, However since its evaluated as a String and not an Integer it will throw an error exception, thats why the Cint() is included (This may also be used to convert it to integer Ctype(number, Integer) Not sure if there is an execution speed difference or not, however Cint() is a faster way of writing it.) it will try and convert the String into an Integer and when its converted into an integer it can be evaluated like one using <>. No one is going to write a whole solution out for you, but while you attempt to create it yourself and more information can be added we are more than happy to help you with problems along the way.

Visual basic - my string comparison function not working correctly

I have data like: (id,volume,median.percantile)
1 Normal Normal Low
2 Low Normal High
3 High High Normal
What I need to do is to have a counter that it increases by 1 when it is not Normal. So I expect
1 Normal Normal Low Counter:1
2 Low Normal High Counter:2
3 High High Low Counter:3
To provide it I wrote the function below. However, when I send there (Normal,Normal,High), it is calculating it 2 instead 1. So it is not working correctly. Where is my mistake?
My code is below:
Public Function getPoint(ByVal volume As String, ByVal median As String, ByVal percantile As String) As Integer
Dim Total As Integer = 0
If volume = “Normal” then
total = total + 1
end if
If median = “Normal” then
total = total + 1
end if
If percantile = “Normal” then
total = total + 1
end if
return total
End Function
In the first record, both volume and median are "Normal". So both of these conditions are true:
If volume = "Normal" Then
total = total + 1
End If
If median = "Normal" Then
total = total + 1
End If
Thus, total gets incremented twice. If you want it to be incremented only once (and essentially ignore the remaining comparisons) then use ElseIf to stop the comparisons once a condition has been satisfied:
If volume = "Normal" Then
total = total + 1
ElseIf median = "Normal" Then
total = total + 1
End If
Or just put them into a single condition:
If volume = "Normal" Or median = "Normal" Then
total = total + 1
End If
Note that this behavior is exactly the opposite of what you describe:
increases by 1 when it is not Normal
A typo perhaps?
What you are doing is opposite to what you want.
You want the count of not-Normal whereas what you are counting and returning the total is Normal.
Simple fix would be return (3 - total).
Btw, is this VBA or VB.Net? Both are based on VB but are different.
If it's VBA for Excel, you can achieve this using simple count formula (3 - count of Normal).
Change your "="'s to "<>"
i.e.
If volume <> "Normal" then
total = total + 1
End if

Random number macro does not return desired value

I have created a simple module that is meant to do following:
generate random number between 0 and 999;
if number is lower than 500, assign value "lower", otherwise "higher"
write the random number and assigned value in cells A1 and B1
repeat the process 100,000 times.
Problem is, it returns assigned value "lower" even if the number is higher than or equal to 500, i.e., all assigned values, 100,000 of them, are "lower"!
I'd appreciate if someone can check where I'm going wrong with this code; I'm not an expert in VBA, but I thought I could do this myself... :\
Sub MacroRanNum()
Dim RunNum As Integer
Dim Outcome As String
For i = 1 To 100000
Randomize
RanNum = Int((999 - 0 + 1) * Rnd + 0)
If RunNum < 500 Then
Outcome = "Lower"
ElseIf RunNum >= 500 Then
Outcome = "Higher"
Else
Outcome = "Error!"
End If
Sheets("podatak").Cells(i, 1) = RanNum
Sheets("podatak").Cells(i, 2) = Outcome
Next i
End Sub
Your variable name is RanNum but you check for RunNum
OPTION EXPLICIT could help to avoid such problems.
You are mixing your variable names. You define and check against RunNum but your random value and your display value are RanNum. You are never testing against the value you randomly generated.

Get the least of a series of numbers WITHOUT an array (VB.NET)

Yes, this is part of a homework assignment. And that's the trouble - I know I could use an array, but we haven't reached that part of the class yet, so I'm not supposed to use that yet. The user inputs five numbers (Doubles, using InputBoxes) and the program should drop the lowest value and calculate the average of the remaining four. At this present juncture, we've been studying For...Next loops, and this is the corresponding For...Next section in the book, so I know I'm supposed to incorporate one in here somewhere, but I have yet to figure out how to calculate and then keep a running "lowest" variable.
Thanks for the help!
Original:
Sub getAverage()
Dim first As Double = CDbl(InputBox("Enter the first grade.", "First"))
Dim second As Double = CDbl(InputBox("Enter the second grade.", "Second"))
Dim third As Double = CDbl(InputBox("Enter the third grade.", "Third"))
Dim fourth As Double = CDbl(InputBox("Enter the fourth grade.", "Fourth"))
Dim fifth As Double = CDbl(InputBox("Enter the fifth grade.", "Fifth"))
Dim min As Double = 0
Dim sum As Double = 0
For count As Integer = 1 To 5
Next
Dim average As Double = 0
txtOutput.Text = average.ToString("N2")
End Sub
Edit 1 (Thanks Guffa): What should I compare the current grade to?
Sub getAverage()
Dim min As Double = 0
Dim sum As Double = 0
For count As Integer = 1 To 5
Dim grade As Double = InputBox("Enter Grade #" & count)
sum += grade
If grade < '??? Then
min = grade
End If
Next
Dim average As Double = 0
txtOutput.Text = average.ToString("N2")
End Sub
Don't have five input statements, put one input statement in the loop instead.
You would need to keep track of:
- The sum of all values
- The lowest value that you encounter
Just add each numbers to the sum as they are entered, and update the lowest value if the entered value is lower than what you have got before.
When the user has entered all the numbers, you just subtract the lowest number from the sum of all the values, that gives you the same value as if you had added up all the numbers except the lowest one.
The average is just the sum divided by the number of values.