[Solved] What are Happy Numbers and how can I print them in C#?

  

2
Topic starter

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)...

1 Answer
2

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);
                        }
                    }
                }
            }
 
        }
    }
}
Share: