Q:

Write a C# Sharp program to check whether a given number is perfect number or not

0

Write a C# Sharp program to check whether a given number is perfect number or not

Sample Output:

Check whether a given number is perfect number or not:                                                                                                         
--------------------------------------------------------                                                                                                       
Input the  number : 20                                               
The positive divisor  : 1  2  4  5  10                                                                                                         
The sum of the divisor is : 22                                                                                                         
So, the number is not perfect.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/*Perfect number is a positive number which sum of all positive divisors excluding that number is equal to that number. For example 6 is perfect number since divisor of 6 are 1, 2 and 3.  Sum of its divisor is 1 + 2+ 3 = 6*/
using System;  
public class Exercise27  
{  
    public static void Main()
{
  int n,i,sum;
  
	Console.Write("\n\n");
    Console.Write("Check whether a given number is perfect number or not:\n");
    Console.Write("--------------------------------------------------------");
    Console.Write("\n\n");    

   Console.Write("Input the  number : ");
   n= Convert.ToInt32(Console.ReadLine());  
    sum = 0;
   Console.Write("The positive divisor  : ");
    for (i=1;i<n;i++)
      {
      if(n%i==0)
         {
         sum=sum+i;
         Console.Write("{0}  ",i);
         }
       }
    Console.Write("\nThe sum of the divisor is : {0}",sum);
    if(sum==n)
      Console.Write("\nSo, the number is perfect.");
    else
      Console.Write("\nSo, the number is not perfect.");
    Console.Write("\n");
  }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now