2 Replies - 338 Views - Last Post: 03 November 2012 - 03:24 PM Rate Topic: -----

#1 las8978  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-October 12

For...Next Loop & calculation error

Posted 03 November 2012 - 10:35 AM

I'm a beginner with programming & am taking a course in VB2010. The program (Windows Forms Application) I've been working on is a number classifier and there's an error with my calculation (my code is a mess) so the answer does not appear in the Output text box like it should. My instructor said if the For...Next loop's index = 2, then add n; if it divides, then add it the sum.
Its purpose is to:
1. take an integer entered by the user in a text box (named txtInput) and
2. perform a calculation and determine whether the number classification is:
a. a perfect number (the sum of the integer's divisors equals the original number),
b. an abundant number (the sum of the number's divisors is greater than the original number), or
c. a deficient number (the sum of the number's divisors is less than the original number), and
3. displays the number's class in a text box (named txtOutput).

Could someone please point me in the right direction? I'm very confused and frustrated. I've been searching for help for a couple days but can't find anything other than similar programs written in C, C++ or Java.

Public Class Form1
   
    Private Sub btnClassify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClassify.Click

        Dim intCount As Integer
        Dim n As Integer        ' number entered by user
        Dim m As Integer        ' counter? or is j the counter?
        Dim half As Integer     ' holds the divisors
        Dim sum As Integer = 1  ' holds the sum of the divisors

        For j = 2 To m
            n = CInt(n).ToString
            half = (n \ 2 Mod 0)
            sum = half * (2 * n + (n - 1) * m)
            If n = sum Then
                txtOutput.Text = "perfect"
            ElseIf n < sum Then
                txtOutput.Text = "abundant"
            ElseIf n > sum Then
                txtOutput.Text = "deficient"
            End If
            intCount += 1
        Next j

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtInput.Clear()
        txtOutput.Clear()
        ' give focus to the first textbox
        txtInput.Focus()

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
End Class


Posted Image

Is This A Good Question/Topic? 0
  • +

Replies To: For...Next Loop & calculation error

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: For...Next Loop & calculation error

Posted 03 November 2012 - 01:44 PM

View Postlas8978, on 03 November 2012 - 11:35 AM, said:

I'm a beginner with programming & am taking a course in VB2010. The program (Windows Forms Application) I've been working on is a number classifier and there's an error with my calculation (my code is a mess) so the answer does not appear in the Output text box like it should. My instructor said if the For...Next loop's index = 2, then add n; if it divides, then add it the sum.


Your first problem is that you are not actually using the number entered by the user. For some reason, you are under the erroneous assumption that 'n' contains the number you want to test.

More to come...

This post has been edited by lar3ry: 03 November 2012 - 01:51 PM

Was This Post Helpful? 0
  • +
  • -

#3 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: For...Next Loop & calculation error

Posted 03 November 2012 - 03:24 PM

OK... I'm back...

There are a few fundamentals you will need to think about when planning your solution.

You have a comment asking if m or j is the loop counter. You should probably have a look in your textbook to see what, exactly, a For loop is. Your choice of using a For loop to find the sum of factors is a good one, but you first have to understand why it is.

The primary advantage of a For loop is to increment (or decrement) a counter. So you should ask yourself what you should be using the loop counter for. In this case, the loop counter should probably contain something that can be used to test to see if its value is a factor of the input number.

Consider this bit of pseudo-code:

Definition of a perfect number: A number whose divisors smaller than itself add up to the number itelf.
Smallest example: 6, whose factors smaller than itself are 1, 2, and 3, so 3+2+1 = 6. Perfect!

Method:
generate all numbers from 1 to half the test number (integer divide to find half)
see if each generated number divides evenly into the test number
if it does, add it to the sum
if sum is equal to the input number, call it perfect.

To see if your pseudo-code is correct, use the smallest example to test it by thinking through the process, then try doing the same thing for a non-perfect number or two.

Now, keeping all that in mind, write some code, and get back to us with tales of your success (or further problems).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1