Using java to find Happy Numbers: Nested while loop loops indefinitely -
My assignment asks when asked for nested input, while searching for loops that a number is a happy number or No. So far I have this:
int i = 0; Int sum = 0; Int dig2, dig1, dig3, dig4, dig1next, dig2next, dig3next; Int digit1sum, digit2sum, digit3sum; HappyNumber = number; While (i & lt; 500) {While (happy number> gt; 0) {while (zodiac! = 1) {dig3 = happyNumber / 100; Dig2 = Happy Numbers% 10; Dig1 = Khushanber / 10; Dig2next = dig2% 10; Dig1next = dig1% 10; Dig3next = dig3% 10; Digit1sum = dig1next * dig1next; Digit2sum = dig2next * dig2next; Digit3sum = dig3next * dig3next; Sum = digit1sum + digit2sum + digit3sum; HappyNumber = sum; } System.out.println ("This is a happy number."); System.exit (0); } I ++; Println (i); System.exit (0); }
I & lt; 500 is set, when I reach ++500, loop should be stopped. I've tried a lot to put + in every section of code, it never works, what am I doing wrong here?
Also: I do not have permission to use this for loops or two ends on this work. I only have to use nested during loops
Happy number < / Strong>: To start with any positive integer, replace the number of digits with the number of numbers, and repeat the process until the number is equal to 1 (where it resides), or is it a cycle Laps endlessly in A. 1 (How will loop long: 500).
After a quick look at your code:
! = 1) .... sum = digit1sum + digit2sum + digit3sum; HappyNumber = sum;
This test is always likely to be true -> infinite loop -> stack overflow
Comments
Post a Comment