The program requires input of positive numbers, and counts each even number using a loop function that doesn't count odds and ends if a O is input.
I'm not sure how to go about creating a loop or if I can use an if function within the loop.
Dim Count = 0
While (number mod 2 = 0) do
Count + 1 = Count
I actually didn't understand the question very well but as far as concerned, if you dont want odd numbers to be included I suggest on count add 2 not one , since the count variable starts with zero do:
Dim Count+2
Btw when do you want the count to stop? At 2 And goes back to 0?
If so then use the if statement
var Dim_count = 0;
if(Dim_count == 0){Dim_count+2}
else if(Dim_count ==2){Dim_Count =0;}
It would help if you provide a sample input so we can work with actual code and guide you to the right solution.
If you receive the input as, let's say, array of numbers, you can simply loop trough it using for or foreach and add additional condition to check for 0 if you want to preliminary exit:
For Each number As Integer In numbers
If (number mod 2 = 0) Then
Count = Count + 1
End If
If (number = 0) Then
Exit For
End If
Next
If you have existing code in which somehow number is reinitialized/redefined on each iteration already, then what you have is pretty close to what you need:
While (number <> 0)
If (number mod 2 = 0) Then
Count = Count + 1
End If
End While
Function counts even numbers:
REM function gets input, exits at 0 and adds positive even numbers.
DO
INPUT X
IF X = 0 THEN PRINT Y; " even numbers": END
IF X > 0 THEN
IF X / 2 = X \ 2 THEN Y = Y + 1
END IF
LOOP
I am completely unsure about this. just a guess...
Dim j as Integer
Dim i As integer
j = 0
i = 2
For i = 1 to 100
j = j+i
Print j
Loop
End Sub
Assuming you are getting numbers from some input, this is how you can do it. Have an infinite loop with While True, then for every number given from your input, check if its even using number mod 2 = 0. This will go on forever so you need to add some condition (another if statement) for it to stop the while loop. More information about while loops here: https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/while-end-while-statement
Dim Count = 0
While True do
If (number mod 2 = 0) Then
Count + 1 = Count
End If
End While
Related
[Sorry in advance, I'm very new to programming.]
This is for project euler problem #2. The goal is to calculate the sum of all even fibonacci numbers that do not exceed 4 million. For anyone not familiar with fibonacci numbers, a fibonacci number is simply a number that is the sum of the two previous numbers in the sequence. For example, the first few fibonacci numbers are 1,2,3,5,8,13,21,34 ...
My code is below beginning with some variables, then my while loop, and finally my for loop.
n = 0
n2 = 1
fibsum = 0
fibrange = range(1,4000001)
while (n2 <= 4000000):
n2 = n2 + n
n = n2 - n
if n2 % 2 == 0:
fibsum += n2
print (fibsum)
# for n2 in fibrange:
# n2 = n2 + n
# n = n2 - n
# if n2 % 2 == 0:
# fibsum += n2
# print(fibsum)
As I said, my while loop works like a charm, but when I run the for loop the output of fibsum is 0. So the value is not changing at all.
I've tried range (1, 4000001) in place of fibrange. I really have no idea what else to try. This is like my 4th or 5th program ever.
Using for loop in range function, The Value of the variable n2 changes according only to range function.You cannot manually change the variable in for loop while using range function.
for and while and are different types of loops.
while: Repeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body.
for: Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
With while you can do something like this:
a = 0
b = 1
target = 4000000
result = 0
while a <= target:
if a % 2 == 0:
result +=a
a, b = b, a + b
print(result)
With for:
a, b = 0, 1
result = 0
target = 4000000
fib_sequence = 35 # -> the length of fibonacci sequence
for _ in range(fib_sequence):
if a % 2 == 0:
result +=a
a, b = b, a + b
if a >= target: break
print(result)
I am getting the error "End If Without Block If" problem in VBA. Followin is my code. Could someone help me find out the mistake I am making?
Function ProjectedProductionPlan(Coverage As Double, Sales As Variant, ProjectedStock As Double) As Double
Dim count As Integer
Dim ResidualBalance As Double
Dim ProjectedPlan As Double
Dim k As Integer
Dim x As Integer
Dim y As Single
Dim s As Single
count = Sales.count
s = 0
ResidualBalance = ProjectedStock
i = 1
If Coverage < 1 Then
ProjectedPlan = (Sales(i) * Coverage) - ResidualBalance
ElseIf Coverage = 1 Then
ProjectedPlan = Sales(i) - ResidualBalance
Else
For k = 1 To count
Do Until k - Coverage > 0
x = k
y = Coverage - x
s = Sales(k) + s
Loop
Exit For
End If
ProjectedPlan = s + (Sales(x + 1) * y)
End Function
As per my comment:
To get rid of the compile error, you'll need to change Exit For into Next to create an actual iteration whereas Exit For is simply a statement within the loop to Exit the loop. Here is MS-documentation on For...Next loops.
However, you also make use of a Do Until...Loop. As I see your code, repairing the above would immediately throw your code in an infinite loop since no variable (neither k nor coverage) get's adjusted. So my suggestion would be to include an IF statement inside your For...Next loop instead. For example like this:
For k = 1 To count
If k - Coverage > 0 Then
Exit For
Else
x = k
y = Coverage - x
s = Sales(k) + s
End If
Next
I've not checked the rest of your code to see if implementing this is actually what you needed.
Small sidenote: using Integer data type variables is only going to bite you at one point. Use Long instead.
I tried to put seconds in 2 text-boxes, each digit in one. Example x= 56 x1= 5 and x2= 6
' s = TimeOfDay.Second
TextBox15.Text = s.Substring(0, 1)
TextBox16.Text = s.Substring(1, 1)'
When I try this I get the following error: System.ArgumentOutOfRangeException
Any ideas on how to fix this?
ArgumentOutOfRange exceptions occurs whenever you attempt to get a character that doesn't exist at the given position. So what is happening is that there is either not a String at position 0 with a length of 1 or there is not a String at position 1 with a length of 1.
To prevent this, add a simple If/Then statement to check if the length of the original String at least equal to the position of the character. Also for what it's worth, since you only want one letter, simply get the character at the desired index of the String.
Here is a quick example:
If s.Length >= 1 Then
TextBox15.Text = s(0).ToString()
End If
If s.Length >= 2 Then
TextBox16.Text = s(1).ToString()
End If
Fiddle: Live Demo
You don't need to convert it to a string before getting the digits, just doing the maths to get them will work well enough:
Dim rightNow = DateTime.Now
TextBox15.Text = (rightNow.Second \ 10).ToString()
TextBox16.Text = (rightNow.Second Mod 10).ToString()
And another approach.
Dim c() As Char = DateTime.Now.Second.ToString("00").ToArray
TextBox1.Text = c(0)
TextBox2.Text = c(1)
I essentially have two list containing strings:
com = ['746365', '6365']
dec = ['6d955s2359d757bb40d0cf36bd7a35662d8b3']
I take the length of the first element in list a '746365' and cut list b into the same length as the first element, (len('746365') = 6) and the result of b is '6d955s'. I now wish to move along the element in b by one place until i reach the end ('62d8b3').
I currently have:
count = 0
for a in com:
for b in dec:
print(com.index(a), a)
length_of = len(a)
print(b[0 + count:length_of + count])
count = count + 1
This doesn't work. However if I remove the count parts and leave whats displayed beneath it works but I can't get it to move along by one
for a in com:
for b in dec:
print(com.index(a), a)
length_of = len(a)
print(b[0:length_of])
how can I adjust this code so that I print then move along by one and print the next?
The initial result should be:
6d955s d955s2 955s23 etc etc..
once the end of dec has been reached, it'll move onto the next value in com.
Thanks.
Try this:
for a in com:
for b in dec:
for i in range(0,len(b)-len(a)+1):
clipped_str = b[i:i+len(a)]
print(clipped_str)
Try this code:
start = 0
for c_element in com:
current_length = len(c_element)
end = start + current_length
for d_element in dec:
for _ in range(len(d_element) - current_length + 1):
print(d_element[start:end])
start += 1
end += 1
else:
start = 0
I have an issue where I am trying to compare a values that can be alphanumeric, only numeric, or only alphabetic.
The code originally worked fine for comparing anything within the same 100s group (IE 1-99 with alphabetic components). However when I included 100+ into it, it malfunctioned.
The current part of the code reads:
For j = 1 To thislength
If lennew < j Then
enteredval = Left("100A", lennew)
ElseIf lennew >= j Then
enteredval = Left("100A", j)
End If
If lenold < j Then
cellval = Left("67", lenold)
ElseIf lenold >= j Then
cellval = Left("67", j)
End If
'issue occurs here
If enteredval >= cellval Then
newrow = newrow+1
End If
Next j
The issue occurs in the last if statement.
When cycling through the 100 is greater than the 67 but still skips over. I tried to declare them both as strings (above this part of code) to see if that would help but it didn't.
What I am trying to accomplish is to sort through a bunch of rows and find where it should go. IE the 100A should go between 100 and 100B.
Sorry lennew=len("100A") and lennold=len("67"). And thislength=4or whatever is larger of the two lengths.
The problem is that you're trying to solve the comparison problem by attacking specific values, and that's going to be a problem to maintain. I'd make the problem more generic by creating a function that supplies takes two values returns -1 if the first operand is "before" the second, 0 if they are the same, and 1 if the first operand is "after" the second per your rules.
You could then restructure your code to eliminate the specific hardcoded prefix testing and then just call the comparison function directly, eg (and this is COMPLETELY untested, off-the-cuff, and my VBA is VERRRRRY stale :) but the idea is there: (it also assumes the existence of a simple string function called StripPrefix that just takes a string and strips off any leading digits, which I suspect you can spin up fairly readily yourself)
Function CompareCell(Cell1 as String, Cell2 as String) as Integer
Dim result as integer
Dim suffix1 as string
Dim suffix2 as string
if val(cell1)< val(cell2) Then
result = -1
else if val(cell1)>val(cell2) then
result = 1
else if val(cell1)=val(cell2) then
if len(cell1)=len(cell2) then
result =0
else
' write code to strip leading numeric prefixes
' You must supply StripPrefix, but it's pretty simple
' I just omitted it here for clarity
suffix1=StripPrefix(cell1) ' eg returns "ABC" for "1000ABC"
suffix2=StripPrefix(cell2)
if suffix1 < suffix2 then
result = -1
else if suffix1 > suffix2 then
result = 1
else
result = 0
end if
end if
return result
end function
A function like this then allows you to take any two cell references and compare them directly to make whatever decision you need:
if CompareCell(enteredval,newval)>=0 then
newrow=newrow+1
end if