Write a program in C# to find the perfect numbers between 1 and 500.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
static void Main(string[] args) { Console.WriteLine("Find the perfect numbers between 1 and 500:"); Console.WriteLine("------------------------------------------------"); int i = 1, u = 1, sum = 0; Console.WriteLine("The perfect numbers between 1 to 500 are:"); while (i <= 500) { while (u <= 500) { if (u < i) { if (i % u == 0) sum = sum + u; } u++; } if (sum == i) { Console.Write(i + " "); } i++; u = 1; sum = 0; } Console.ReadKey(); } |
Output: