# Finding happy numbers.

Page 1 of 1

## 8 Replies - 4061 Views - Last Post: 05 October 2014 - 09:56 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=355164&amp;s=8c66c3d8b4e8553b5462f4d844fe777b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• Posts: 13
• Joined: 29-September 14

# Finding happy numbers.

Posted 05 October 2014 - 06:37 PM

I'm currently working on an assignment that finds all happy numbers for a given range. Such as 1- 10. My current code just isn't cutting it. I have to use a user defined function and return a bool value to main in order to output if the number is happy or if it sad. For some reason, I cannot get it to loop properly. the first number will be displayed correctly, but the second and third are just squares of the first number, instead of being squared by their single digits. I'm dividing by 10 to get the first number of the digit entered, then I'm using mod to get the remainder.
one thing I did notice was that the num2 was putting a remainder of 30 for 130, and I'm not too sure what I need to do to change that, without affecting the whole program.

Any help would be fantastic, thanks.

```#include <iostream>
#include <iomanip>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdlib>
using namespace std;
/****************************************************************************************

Stephen Allen, CS361, Week 6, Programming assignment 6, Program 1
Program calculates the bill for a user
input is number of puchases, type of iceream, amount of icecream, and number of toppings
output is total for icecream by treat type, weight and topping amount
Total bill amout is outputted by main

I called the janitor the other day to see what he could do about my dingy linoleum floor.
He said he would have been happy to loan me a polisher,
but that he hadn't the slightest idea what he had done with it.
I told him not to worry about it -
that as a programmer it wasn't the first time
I had experienced a buffer allocation failure due to a memory error.

***************************************************************************************/

// constants for max values
const int MIN = 1;
const int MAX = 9999;
const int MAX_TERMS = 50;

const int NINE = 9;
const int TEN = 10;
const int HUND = 100;
const int THOU = 1000;
// constant strings for output
const string FIRST_NUM = "Enter the first number to test: ";
const string LAST_NUM = "Enter the last number to test: ";
const string ERROR1 = "Invalid -- must be between ";
const string ERROR_AND = " and ";

// prototypes
int happy();
int numbers();

bool happy (int input1){

bool test = false;
int sum;
int num1;
int num2;
int num3;
int num4;
int num5;
int div ;
sum = input1;

for (num1 = 0; num1 <= MAX_TERMS || sum != MIN; ++num1){

while ( num1 <= MAX_TERMS && sum != MIN){

if (sum <= 9){
div = MIN;
}
if (sum < 100){
div = TEN;
}
if (sum >= 100){
div = 100;
}

if (sum >= 1000){
div = 1000;
}

num1 = sum / div;
num2 = sum % div;
cout << endl;
cout << num1 << endl;
cout << num2 << endl;
num3 = num1 * num1;
num4 = num2 * num2;
num5 = num3 + num4;
sum = num5;

cout << " " << sum << " ";

}

}

if (sum == MIN ){
test = true;
}

return test;

}

int numbers(int& input1, int& input2){

// prompts user for input for first number
cout << FIRST_NUM;
cin >> input1;
cout << endl;
while (input1 < MIN || input1 > MAX){

cout << ERROR1 << MIN << ERROR_AND << MAX << endl;
cout << FIRST_NUM;
cin >> input1;
}

// prompts for user input for last number
cout << endl;
cout << LAST_NUM;
cin >> input2;
cout << endl;

while (input2 < input1 || input2 > MAX){

cout << ERROR1 << input1 << ERROR_AND << MAX << endl;
cout << LAST_NUM;
cin >> input2;
}

return 0;

}

int main (){

int input;
int input2;
int compare_num;
bool happy_num;

cout << "Program to test for Happy numbers" << endl;
cout << endl;

// calls numbers function to get input from user
numbers(input,input2);

compare_num = input;

while (compare_num <= input2){

happy_num = happy(compare_num);

if (happy_num == 1){
cout << endl;
cout << "Number is happy";
cout << endl;
system ("Pause");

}

else if(happy_num == 0){
cout << endl;
cout << endl;
system ("Pause");

}
++compare_num;

}

system ("Pause");
return 0;
}
```

Is This A Good Question/Topic? 0

## Replies To: Finding happy numbers.

### #2 CTphpnwb

• D.I.C Lover

Reputation: 3782
• Posts: 13,708
• Joined: 08-August 08

## Re: Finding happy numbers.

Posted 05 October 2014 - 08:35 PM

• Fix your indenting/white space. Code that's easy to read is easier to debug.
• Use vectors since you don't know how many digits you'll be dealing with.
• I did this using wikipedia's suggesting that unhappy numbers end up with the number 4 (or 16, 37...) in their sequence at some point as a sentinel.

Reputation: 0
• Posts: 13
• Joined: 29-September 14

## Re: Finding happy numbers.

Posted 05 October 2014 - 08:44 PM

CTphpnwb, on 05 October 2014 - 08:35 PM, said:

• Fix your indenting/white space. Code that's easy to read is easier to debug.
• Use vectors since you don't know how many digits you'll be dealing with.
• I did this using wikipedia's suggesting that unhappy numbers end up with the number 4 (or 16, 37...) in their sequence at some point as a sentinel.

The problem is I can't use vectors. So I'm stuck with using mod

### #4 CTphpnwb

• D.I.C Lover

Reputation: 3782
• Posts: 13,708
• Joined: 08-August 08

## Re: Finding happy numbers.

Posted 05 October 2014 - 08:51 PM

Ok, but you can still sum up the squares of the digits. Actually, it''s easier without vectors!

Reputation: 0
• Posts: 13
• Joined: 29-September 14

## Re: Finding happy numbers.

Posted 05 October 2014 - 09:01 PM

CTphpnwb, on 05 October 2014 - 08:51 PM, said:

Ok, but you can still sum up the squares of the digits. Actually, it''s easier without vectors!

I'm using the mod function to get the remainder to square the digits. The issue is that once I hit a number that is higher than 100 I can't divide that number by 10 any more using the mod, because if 130 % 10 = 30, and I need it to be 130 % 100 = 3.

### #6 CTphpnwb

• D.I.C Lover

Reputation: 3782
• Posts: 13,708
• Joined: 08-August 08

## Re: Finding happy numbers.

Posted 05 October 2014 - 09:05 PM

By the way, your happy function makes no sense to me. What is the purpose of div?

You get digits by using modulus on the base (in this case 10) and then after getting/using the digit, you divide by the base (10) and continue. My entire happy function takes 15 lines including braces — although I did use recursion.

mistawondabread, on 06 October 2014 - 12:01 AM, said:

I'm using the mod function to get the remainder to square the digits. The issue is that once I hit a number that is higher than 100 I can't divide that number by 10 any more using the mod, because if 130 % 10 = 30, and I need it to be 130 % 100 = 3.

Huh? 130 % 10 is 0.

Reputation: 0
• Posts: 13
• Joined: 29-September 14

## Re: Finding happy numbers.

Posted 05 October 2014 - 09:14 PM

CTphpnwb, on 05 October 2014 - 09:05 PM, said:

By the way, your happy function makes no sense to me. What is the purpose of div?

You get digits by using modulus on the base (in this case 10) and then after getting/using the digit, you divide by the base (10) and continue. My entire happy function takes 15 lines including braces — although I did use recursion.

mistawondabread, on 06 October 2014 - 12:01 AM, said:

I'm using the mod function to get the remainder to square the digits. The issue is that once I hit a number that is higher than 100 I can't divide that number by 10 any more using the mod, because if 130 % 10 = 30, and I need it to be 130 % 100 = 3.

Huh? 130 % 10 is 0.

Sorry, I meant 130 % 100 = 30.

I tried to set up if then statements to find if the num2 is greater than 10 to divide it again, but it's not working.

### #8 CTphpnwb

• D.I.C Lover

Reputation: 3782
• Posts: 13,708
• Joined: 08-August 08

## Re: Finding happy numbers.

Posted 05 October 2014 - 09:18 PM

Let's take 130 as an example:
130 % 10 = 0, so the least significant digit is 0. So far our sum of digits squared is 0.
130 / 10 = 13
13 % 10 = 3 So far our sum of digits squared is 0 + 3*3 = 9
13/10 = 1 (remember, this is integer math)
1%10 = 1 So far our sum of digits squared is 9 +1*1 = 10
1/10 = 0 (integer math again) so we're done and our sum is 10.
Repeat for 10 and we see we have a happy number.

mistawondabread, on 06 October 2014 - 12:14 AM, said:

Sorry, I meant 130 % 100 = 30.

But it doesn't need to work that way. See above.

This post has been edited by CTphpnwb: 05 October 2014 - 09:19 PM

Reputation: 0
• Posts: 13
• Joined: 29-September 14

## Re: Finding happy numbers.

Posted 05 October 2014 - 09:56 PM

CTphpnwb, on 05 October 2014 - 09:18 PM, said:

Let's take 130 as an example:
130 % 10 = 0, so the least significant digit is 0. So far our sum of digits squared is 0.
130 / 10 = 13
13 % 10 = 3 So far our sum of digits squared is 0 + 3*3 = 9
13/10 = 1 (remember, this is integer math)
1%10 = 1 So far our sum of digits squared is 9 +1*1 = 10
1/10 = 0 (integer math again) so we're done and our sum is 10.
Repeat for 10 and we see we have a happy number.

mistawondabread, on 06 October 2014 - 12:14 AM, said:

Sorry, I meant 130 % 100 = 30.

But it doesn't need to work that way. See above.

You are amazing. Seriously. I learned more from this post then I did from the 4 chapters of my C++ book I wrote. I hope you are a teacher!

Thank you so much! I wish I could buy you a beer.

I meant read, I did NOT write a C++ book. Ha.