Total the numbers using For loop

Jwiklund

Member
Joined
Oct 13, 2011
Messages
6
Programming Experience
Beginner
Hello I am new to this forum, I have tried to search did not find the answer to my question. I would like to create a "For loop" that summarizes the numbers entered by the user and displays the result. I have come this far:

Public Sub Readinput()


Dim NumOfInt As Integer
Dim i As Integer = 0
Dim numbers As Integer
Dim result As Integer


Console.Write("How many numbers do you want to add? ")
NumOfInt = CInt(Console.ReadLine)


For i = 1 To NumOfInt Step 1


Console.Write("Enter your number: ")
numbers = CInt(Console.ReadLine)




Next


Console.WriteLine(result)


End Sub


End Class

I guess this´s a easy question for most of you but I´m grateful for your help

Best regards

Johan
 
If you by "summarizes the numbers" mean that you want to add the numbers together then you can use the + operator for that.
 
An example might make things clearer:

Let me begin by asking a question to the user:

How many numbers do you want to add? 3

Enter your number: 5
Enter your number: 7
Enter your number: 3

Result: 15

How do I add in this case 5+7+3 using a for loop? and show the result 15 or what ever it might be.
 
You start with a result that is 0, during the loop you add each number to the result.
 
I think I got it! I added:

"result += numbers"

Then I got the loop to add the numbers. It seems easy now but my problem were that I didnt realy understand how the for loop worked . Thanks for your help it got me thinking in the right direction.


Best regards


Johan
 
Back
Top