private static string FindHappyNumbers() { Console.WriteLine("How many happy numbers do you want to find?"); int NumbersToFind = int.Parse(Console.ReadLine()); int[] NumbersFound = new int[NumbersToFind]; int HappyNumbersFound = 0; int startingNum = 1; int num = 1; const int searchLoopLimit = 10; int searchLoops = 0; string numAsString = String.Empty; bool searchEnded = false; while (HappyNumbersFound < NumbersToFind) { if (searchLoops == searchLoopLimit) { searchEnded = true; } if (searchEnded) { ResetLoopWithNextNumber(ref startingNum, ref num, ref searchLoops, ref searchEnded); } numAsString = num.ToString(); int sum = 0; foreach (var digitChar in numAsString) { int digit = int.Parse(digitChar.ToString()); sum += (int)Math.Pow(digit, 2); } if (sum == 1) { NumbersFound[HappyNumbersFound] = startingNum; HappyNumbersFound += 1; searchEnded = true; } else { num = sum; searchLoops += 1; } } string FormattedArray = TurnArrayIntoWellFormattedString(NumbersFound); return FormattedArray; } private static void ResetLoopWithNextNumber(ref int startingNum, ref int num, ref int searchLoops, ref bool searchEnded) { startingNum += 1; num = startingNum; searchLoops = 0; searchEnded = false; } private static string TurnArrayIntoWellFormattedString(int[] numbersFound) { string StringToReturn = ""; int length = numbersFound.Length; for (int i = 0; i < length; i++) { if (i < length - 1) { StringToReturn += numbersFound[i] + ", "; } else { StringToReturn += numbersFound[i]; } } return StringToReturn; }
Here's mine in C#. I feel like this could have been way shorter though. Oh well :p
bro log code is not good......
Thanks for letting me know.
What should I change so that my code improves?
your feedback... your code write is good and logic is also good , but you code very long ,,,
so, listen ,try to write code small, simpal
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Here's mine in C#. I feel like this could have been way shorter though. Oh well :p
bro log code is not good......
Thanks for letting me know.
What should I change so that my code improves?
your feedback...
your code write is good and logic is also good , but you code very long ,,,
so, listen ,try to write code small, simpal