
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Program to Check if a Number is Perfect
What is a Perfect Number?
A perfect Number is a positive integer that is equal to the sum of its proper divisors(excluding itself). For example, 6 is a perfect number because its divisors (1, 2, and 3) sum up to 6. Proper divisors are those numbers that leave no remainder when divided by numbers. The proper divisor should not include that number itself otherwise it would always be greater than a number.Problem Description
In this article, we will learn how to check whether a number is a perfect number or not using C#.Below are a few examples including edge case examples to know about perfect numbers:
Example 1
- Input: Number = 28
- Output: True
Explanation
Given the input 28, the divisors of 28 are1, 2, 4, 7, and 14, now we calculate the sum of all divisors, and we will get a sum equal to 28.
Since the sum of divisors equals N. N is a perfect number.
Example 2
- Input: Number = 15
- Output: False
Explanation
Given the input 15, the divisors of 15 are 1, 3, and 5, now we calculate the sum of all divisors, and we will get a sum equal to 9.
Since the sum of divisors is not equal to N (15 is not equal to 9). N is not a perfect number.
Example 3
Now let's take an example for the greater number.
- Input: Number = 496
- Output: True
Explanation
Given the input 496, the divisors of 496 are 1, 2, 4, 8, 16, 31, 62, 124, and 248, now we calculate the sum of all divisors, we will get a sum equal to 496.
Since the sum of divisors is equal to N. N is a perfect number.
Example 4
- Input: Number = 0
-
Output: False
Explanation
Given the input 0, the divisor of 0 is 1, now we calculate the sum of its divisors, and we will get a sum equal to 1.
Since the sum of divisors is not equal to N (1 is not equal to 0). 0 is not a perfect number.
Approach
Firstly, we will initialize a variable sum to store the sum of divisors. Then we will handle edge case numbers as numbers less than or equal to 0 can never be a perfect number. Now, we create a loop from 1 to N/2 (inclusive), since any divisor of N will be less than or equal to N/2. Inside the loop, we will check if the current number divides N perfectly (N % i == 0). If true, add it to the sum. Finally, return true if the sum of divisors is equal to the number.Steps for Implementation
- Create a function to check the perfect numbers using an iterative approach from i = 1 to i <= number / 2.
- Check for negative numbers and zero.
- Now Loop through potential divisors up to N/2 and check if i is a divisor of number.
- If it is divisor add it to sum.
- Check if the sum is equal to the number then return true else return false.
Implementation Code
using System; class Program { // Function using iterative approach static bool IsPerfectNumberIterative(int number) { // edge cases if (number <= 0) return false; int sum = 0; for (int i = 1; i <= number / 2; i++) { if (number % i == 0) { sum += i; } } return sum == number; } static void Main(string[] args) { // input int number = 6; bool answer = IsPerfectNumberIterative(number); if (answer) { Console.WriteLine($"{number} is a perfect number."); } else { Console.WriteLine( $"{number} is not a perfect number."); } } }
Output
6 is a perfect number.
Time Complexity: O(n), where n is the input number we take.
Space Complexity: O(1), constant space