# Help with Happy Numbers Program VB

Page 1 of 1

## 4 Replies - 1731 Views - Last Post: 17 February 2013 - 11:11 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=312533&amp;s=2fba9c00d02d5b34899c691dbd17f7e6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 gatorbait5566

Reputation: 0
• Posts: 3
• Joined: 17-February 13

# Help with Happy Numbers Program VB

Posted 17 February 2013 - 02:50 PM

I need help on writing a program to determine whether or not a given number is a happy number.

Happy number:
add the square of the digits of the number. If the number you get now is equal to 1, then it is happy. If not, you continuethis process until you either get a number that is equal to 1, or you will find yourselves trapped in a sequence of numbers. For example, 19 is a happy number and the sequence looks like this:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1.

Also the limitation is for a number between 1 and 999.
It is a Windows Form Application with a textbox, button, and label.

The number goes into the textbox, you press the button, and then the label tells whether or not it is a happy number. Here is what I have but it is not working and I do not know where to go:

```Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim x As Integer
x = TextBox1.Text
If (TextBox1.Text <= 0) Or (TextBox1.Text >= 1000) Then
Label1.Text = "Please select a number between 1 and 999"
End If

End Sub

Private Function fun1(ByVal x As Integer) As Integer
Dim sum, y As Integer
sum = 0
Do Until sum = 1
y = x Mod 10
sum = sum + y * y
If sum = 1 Then
Label1.Text = "Happy Number"
End If
If sum <= 1 Then
Label1.Text = "Not a happy number"
End If
Loop
Return y

End Function

```

Is This A Good Question/Topic? 0

## Replies To: Help with Happy Numbers Program VB

### #2 tlhIn`toq

• Not here as much anymore

Reputation: 6010
• Posts: 13,179
• Joined: 02-June 10

## Re: Help with Happy Numbers Program VB

Posted 17 February 2013 - 03:46 PM

We can't give you code to fix it as this is course homework.

"Its broke" doesn't give us much to go on other than you can't debug your own code, so here are several tutorials on debugging:

Also:
"I don't know where to go/start" - This usually means you should go back to your instructor and admit you are this lost. Don't bluff your way through this course thinking that by chapter 10 it will all suddenly snap into place and become clear. It won't. Unlike history class where chapter 1 might be 17th century England and chapter 2 might be World War II, giving you a fresh start - Coding builds upon the lessons of the previous chapter. You have to use lesson 1 material to succeed in lesson 2. Chapter 10 builds upon and uses material from chapter 9. If you let your pride get in the way you will be too lost to recover and have wasted thousands of dollars in tuition.

tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.

What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2

FAQ (Frequently Asked Questions - Updated Jan 2013
Spoiler

### #3 IronRazer

• D.I.C Lover

Reputation: 672
• Posts: 1,858
• Joined: 01-February 13

## Re: Help with Happy Numbers Program VB

Posted 17 February 2013 - 03:50 PM

I am not sure what you mean by (it is not working). Is it that the math functions are not doing what you want or that you can`t figure out how to call the function or what ?

### #4 lar3ry

• Coding Geezer

Reputation: 312
• Posts: 1,295
• Joined: 12-September 12

## Re: Help with Happy Numbers Program VB

Posted 17 February 2013 - 08:29 PM

It took me a while to figure out exactly what the examnple given was all about. Finally I got it. It should be:

12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1

### #5 IronRazer

• D.I.C Lover

Reputation: 672
• Posts: 1,858
• Joined: 01-February 13

## Re: Help with Happy Numbers Program VB

Posted 17 February 2013 - 11:11 PM

lar3ry, on 17 February 2013 - 10:29 PM, said:

It took me a while to figure out exactly what the examnple given was all about. Finally I got it. It should be:

12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1

I scratched my head for about 15 minutes and could not get it. You must have thought about that for a while.