I have to write a program to compute a happy number. A number is called a happy number, if you repeat the process, of squaring the sum of the digits, till the value 1 is obtained. E.g. You need to do the following to perform this check: (a) compute the sum of the squares of its digits (b) if the resultant value is 1, then the number is a happy number, else execute point (a). If a number is not a happy number, there will be an endless loop/cycle to this execution.
Task: In this programming assignment, I am required to write code that checks whether the number is a happy number or not, for 10 cycles (iterations) only. 2 examples of happy numbers (limited to 10 cycles ) are 19 ( happy number on 4th iteration) and 3 (not happy number after 10 iterations).
So far I have written the following code covering part a), but am stuck on how to proceed afterwards:
#include <iostream>
using namespace std;
int main () {
int i, number_10, resultant, digit_1, digit_10, digit_100; // variable declarations
cout << "Choose number: ";
cin >> number_10; // number input
for (i=1; i<=10; i++) { // for loop for 10 iterations
digit_1=number_10%10; // chooses ones
digit_10=number_10/10; // chooses tenths
digit_100=number_10/100; // chooses digit of hundreds
resultant=digit_1*digit_1+digit_10*digit_10+digit_100*digit_100;
cout << resultant << endl;
if (resultant == 1) {
cout << "Happy number: " << resultant << endl;
break; }
else { number_10 = resultant; } // assign the calculated resultant value to number_10 for next iteration
}
return 0;
}
Can you please tell me where I am doing something wrong or perhaps am missing some lines? Thank you so very much for your help!
First of all, when posting code, please use code tags. Click the Fornat button that shows "<>" and paste your code between the tags that are generated for you. If the button doesn't generate tags, type "[ code ]" and "[ /code ]" without the spaces and paste your code between them. This will make is a whole lot easier to review and comment on your code.
Does your program as you have written it work correctly for happy numbers?
You probably want to save off your original input so when you print int out at the end it hasn't been overwritten. Right now your success line will always be "Happy number: 1"
Based on what I see here, it looks like all you have to do is detect whether a happy number was found and if not, print out a failure message before returning. You are looping on i=0 to i=10. At i=10, the loop ends, so you could check the value of i to see if you completed the loop (no happy number) or broke out early (found a happy number).
By the way, why is your variable named "number_10"? Since it could have 3 digits number_10 is pretty confusing. You might want to read in a value named, for instance" "number" and process a value named, for instance, "currentValue".
Thank you doug4 for your comments. I will take them into account when posting it next time. As for you comments, the program doesn't work properly, as when I input 19 it gives out 82 (correct), then 68 (correct), then 100 (correct), which is ok, but when the digits of 100 are squared and summed instead of giving a happy number, 1, it gives 101 (incorrect). I believe the problem is with the line digit_10=number_10/10, which gives 10 (10 as a digit of a number 100), but should give only 0. In other words, the program for the numbers 19 and 3 does not work properly. The problem arises when it reaches 3-digit number, like 100 as the digit sum for that number then is 1^2+10^2 +0^2 giving 101.
What you mean by saving the original value? I save the modified value, resultant, into the original variable int number_10 after each iteration.
Well, I have written the code to detect if the happy number was found: if (resultant == 1) cout << "Happy number: " << resultant << ends;, and if not then I wrote else { number_10 = resultant } to overwrite the number_10 value with the new value obtained by resultant, which multiplies the squares of digits and adds them together to obtain the value.
Variable is named number_10 because I chose it like that when I first written the code. It took only values of 2-digits and 1-digits (19 or 3 for example). I agree with you that it's confusing. I changed it to number now.
I am sorry for asking such silly questions, but I am a beginner at c++, so for me everything is a steep learning curve and even easy exercises like this take time. I apologise for any inconvenience I might cause you and thank you for your help.