settingsAccountsettings
Menusettings

Q: What are Happy Numbers and how can I print them in C#?

+6 votes

I need to print all 4 digit numbers in format ABCD such as A + B = C + D (known as happy numbers) (from 1 to 9999)...

asked in C# category by user samfred5830
edited by user golearnweb

1 Answer

+1 vote
 
Best answer

Here you can read more about happy numbers: https://en.wikipedia.org/wiki/Happy_number

Regarding the code - try with nested for loops and one if statement.

Something like this:

using System;
class HappyNumbers
{
    static void Main()
    {
        for (int a = 1; a <= 9; a++)
        {
            for (int b = 0; b <= 9; b++)
            {
                for (int c = 0; c <= 9; c++)
                {
                    for (int d = 0; d <= 9; d++)
                    {
                        if (a + b == c + d)
                        {
                            Console.WriteLine("{0}{1}{2}{3}", a, b, c, d);
                        }
                    }
                }
            }

        }
    }
}
answered by user paulcabalit
selected by user golearnweb
...