A Perfect Number is a positive integer for which the sum of all of its positive factors except the number results in the number itself. For example, 6 is a perfect number because the factors of 6 excluding itself are 1, 2, 3 and sum of them is itself - 6. To achieve this, we shall make use of the factors logic and find the sum of all factors except the number itself and check if the sum is equal to the number or not.

```
public static bool IsPerfectNumber(int n)
{
int sum = 0;
Console.Write("Factors:");
for (int i = 1; i < n; i++)
{
if (n % i == 0)
{
Console.Write($"{i} ");
sum = sum + i;
}
}
Console.WriteLine($"Sum: {sum}");
return sum == n ? true : false;
}
```

**How it works:**

While calculating the factors, we exclude the input number from the loop (the reason why the condition is i < n and not i <=n) and for each time the counter is a factor of the input number we add it to the sum. Finally, we compare the sum with the input number for equality and the result tells us if its a Perfect Number or not.

```
Number: 6
Factors:1 2 3 Sum: 6
True
```

Special Numbers Loops • Added 2 months ago

Find if the given number is a Perfect Number

Special Numbers
Loops

Find if the given number is a Palindrome

Special Numbers
Loops

Find if the given number is an Armstrong Number

Special Numbers
Loops

Find if the given number is a Perfect Square

Special Numbers
Loops

Find all the Fibonacci numbers for a given limit

Special Numbers
Loops

Find the number of occurrences of a given character in a string

Strings
Loops

Find the reverse of given number

Loops

Find the Factorial of a given number using Recursion

Recursion
Loops

Check if a given string is Palindrome

Strings
Loops

Find if the given number is a Perfect Number

Special Numbers
Loops

Subscribe to my newsletter and get the first copy of every new post delivered straight into your inbox.

Externalizing Configurations to AWS SSM Parameter Store and using in ASP.NET Core API

AWS
ASP.NET Core

Working with Options pattern in ASP.NET Core - The Complete Guide

ASP.NET Core

Creating AWS Lambda Layers in ASP.NET Core - Getting started

AWS
ASP.NET Core

Deploying Express.js API to AWS Lambda and API Gateway

NodeJS
Express
MySql
AWS

Securing ASP.NET Core APIs with IdentityServer4 - Getting Started

ASP.NET Core
IdentityServer4