Home > Net >  The sum of the first natural 100 numbers/ first even and odd 100 numbers
The sum of the first natural 100 numbers/ first even and odd 100 numbers

Time:10-17

new to coding, Quick Homework problem Im having trouble on. I must find the sum of the first 100 natural numbers. 1 2 3 4, and so on. While using "while loops".

This is what I have so far.

    Dim sum, count As Integer
    Const num As Integer = 1

    Console.WriteLine("The Sum of the first 100 natural numbers is:") 

 

    count = num   1

    While count <= 100 
        Count = num   count
        
    End While
        Sum = count

    Console.WriteLine("{0}", sum) 
    Console.ReadLine()

I know for a fact the math and while loop is incorrect, but Im not sure how to fix it. I keep getting 101 as my answer.

CodePudding user response:

The issue is your initialization. Do this:

Const num As Integer = 0

CodePudding user response:

While count <= 100 
    num  = count
    count  = 1        
End While
    Sum = num

CodePudding user response:

You do not need the num constant and the line: count = num 1

 While count <= 100
    sum  = count
    count  = 1
 End While
  • Related