Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C# Program to Check Perfect Number

Posted in C# Tutorial
Updated on Jan 11, 2024
By Mari Selvan
πŸ‘οΈ 285 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
C# Program to Check Perfect Number

Photo Credit to CodeToFun

πŸ™‹ Introduction

In the realm of programming, exploring the properties of numbers is a fascinating task. One such special type of number is a perfect number.

A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself).

In this tutorial, we will delve into a C# program designed to check whether a given number is a perfect number.

The program involves identifying the divisors of the number and determining if their sum equals the original number.

πŸ“„ Example

Let's dive into the C# code that accomplishes this task.

IsPerfectNumber.cs
Copied
Copy To Clipboard
using System;

class PerfectNumberChecker {
  // Function to check if a number is a perfect number
  static bool IsPerfectNumber(int number) {
    int sum = 1; // Initialize sum with 1 (as 1 is always a divisor)

    // Iterate from 2 to the square root of the number
    for (int i = 2; i <= Math.Sqrt(number); i++) {
      if (number % i == 0) {
        // If i is a divisor, add both divisors if they are different
        if (i == (number / i))
          sum += i;
        else
          sum += (i + number / i);
      }
    }

    // If the sum of divisors is equal to the original number, it is a perfect number
    return (sum == number);
  }

  // Driver program
  static void Main() {
    // Replace this value with the number you want to check
    int number = 28;

    // Call the function to check if the number is a perfect number
    if (IsPerfectNumber(number))
      Console.WriteLine($"{number} is a perfect number.");
    else
      Console.WriteLine($"{number} is not a perfect number.");
  }
}

πŸ’» Testing the Program

To test the program with different numbers, modify the value of number in the Main method.

Output
28 is a perfect number.

🧠 How the Program Works

  1. The program defines a class PerfectNumberChecker containing a static method IsPerfectNumber that takes an integer number as input and returns true if the number is a perfect number, and false otherwise.
  2. Inside the Main method, replace the value of number with the desired number you want to check.
  3. The program calls the IsPerfectNumber method and prints the result using Console.WriteLine.

πŸ“ Between the Given Range

Let's delve into the C# code that identifies perfect numbers in the specified range.

Program.cs
Copied
Copy To Clipboard
using System;

class Program {
  // Function to check if a number is perfect
  static bool IsPerfectNumber(int num) {
    int sum = 0;

    // Iterate through potential divisors up to half of the number
    for (int i = 1; i <= num / 2; i++) {
      if (num % i == 0) {
        sum += i;
      }
    }

    // Check if the sum of divisors is equal to the number
    return sum == num;
  }

  // Driver program
  static void Main() {
    Console.Write("Perfect Numbers in the range 1 to 50: \n");

    // Iterate through the range and check for perfect numbers
    for (int i = 1; i <= 50; i++) {
      if (IsPerfectNumber(i)) {
        Console.Write($"{i} ");
      }
    }

    Console.WriteLine();
  }
}

πŸ’» Testing the Program

Output
Perfect Numbers in the range 1 to 50:
6 28 

Run the program to see the perfect numbers in the specified range.

🧠 How the Program Works

  1. The program defines a function IsPerfectNumber that checks if a given number is perfect.
  2. Inside the function, it iterates through potential divisors up to the square root of the number, summing up proper divisors.
  3. The Main function iterates through the range from 1 to 50 and prints the perfect numbers.

🧐 Understanding the Concept of Perfect Number

Before delving into the code, let's understand the concept of perfect numbers. A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself).

🎒 Optimizing the Program

While the provided program is effective, consider exploring and implementing alternative approaches or optimizations for checking perfect numbers.

Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
8 months ago

If you have any doubts regarding this article (C# Program to Check Perfect Number), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy