Sure, here is the C# code to check if a given number is a perfect number or not:
C#
using System;
class PerfectNumber
{
static void Main(string[] args)
{
int number = Convert.ToInt32(Console.ReadLine());
int sum = 1;
for (int i = 2; i * i <= number; i++)
{
if (number % i == 0)
{
if (i * i != number)
{
sum += i + number / i;
}
else
{
sum += i;
}
}
}
if (sum == number)
{
Console.WriteLine("{0} is a perfect number.", number);
}
else
{
Console.WriteLine("{0} is not a perfect number.", number);
}
}
}
This program first reads a number from the user. Then, it uses a for loop to iterate through all the positive divisors of the number. For each divisor, it adds the divisor to a variable called sum. Finally, it checks if the value of sum is equal to the original number. If it is, then the number is a perfect number. Otherwise, the number is not a perfect number.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is the C# code to check if a given number is a perfect number or not:
C#
This program first reads a number from the user. Then, it uses a for loop to iterate through all the positive divisors of the number. For each divisor, it adds the divisor to a variable called sum. Finally, it checks if the value of sum is equal to the original number. If it is, then the number is a perfect number. Otherwise, the number is not a perfect number.