Sunday, January 9, 2011

check number is perfect number or not using c#


here  we can check the number is perfect number or not using the following code.
public bool IsPerfectNumber(int no)
{
            int i, j;
            int sum = 0;

             for (i = 1; i <= no; i++)
             {

                 if (no % i == 0)
                {
                    j = no / i;

                      if (j != no)
                         sum = sum + j;

                  }
             }
             if (sum == no)

                 return true;

             else
          
     return false;

}

No comments:

Post a Comment