Program To Check Whether A Number Is A Perfect Number Or Not

Introduction

 
Perfect numbers are positive integers that are equal to the sum of its factors except for the number itself. In other words, perfect numbers are the positive integers which are the sum of their proper divisors. The smallest perfect number is 6, which is the sum of its proper divisors: 1, 2, and 3.
 
Program To Check Whether A Number Is Perfect Number Or Not
 
Program to check perfect number
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace PerfectNumber  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             int number, sum = 0;  
  14.             string divisors = "";  
  15.             Console.WriteLine("---------------------------------------------------------------");  
  16.             Console.WriteLine("                   Perfect Number                              ");  
  17.             Console.WriteLine("----------------------------------------------------------------");  
  18.             Console.Write("Enter Number To Check :: ");  
  19.             number = int.Parse(Console.ReadLine());  
  20.   
  21.             for (int i = 1; i < number; i++)  
  22.             {  
  23.                 if (number % i == 0)  
  24.                 {  
  25.                     sum += i;  
  26.                     divisors += i.ToString() + "+";  
  27.                 }  
  28.             }  
  29.             divisors = divisors.Remove(divisors.Length - 1, 1);  
  30.             Console.WriteLine("---------------------------------------------------------------");
  31.             Console.WriteLine(divisors + " = " + sum);  
  32.             if (sum == number)  
  33.             {  
  34.                 Console.WriteLine("Hence , " + number + " Is A Perfect Number");  
  35.             }  
  36.             else  
  37.                 Console.WriteLine("Hence , " + number + " Is Not A Perfect Number");  
  38.   
  39.             Console.WriteLine("---------------------------------------------------------------");
  40.             Console.ReadKey();  
  41.         }  
  42.     }  
  43. }  
Code Explanation
  • First, we define three variables number type of integer, sum type of integer, and divisors type of string. We use numbers to get the input of a user, a sum to get the sum of the divisor, and divisors to add all divisors of the numbers in one string.
  • Then we get input from the user and pass it to the number variable by converting it to an integer.
  • Then we iterate a loop from 1 (one) to number – 1.
  • In the loop, we module number by iterate variable I. If the reminder is 0 then add that number in sum and concat value of I in our variable divisors with plus (+) sign.
  • After loop, we remove last one character from the divisors variable because the extra one + is also concat in the string. Here we just show this as all divisors sum is equal to sum for better user understanding.
  • Next, we check if the sum is equal to the user’s input. If yes, then it shows a message that the entered number is a perfect number. Otherwise, the entered number is not perfect number.
  • Then, we use the Console.ReadKey() method to read any key from the console. By entering this line of code, the program will wait and not exit immediately. The program will wait for the user to enter any key before finally exiting. If you don't include this statement in the code, the program will exit as soon as it is run.
Output
 
Program To Check Whether A Number Is Perfect Number Or Not
 
Program To Check Whether A Number Is Perfect Number Or Not
 
I hope you find this blog helpful. If you get any help from this blog, please share it with your friends.