CS - Program to find a given number is Perfect Number or not?

Objective: To find the given number is Perfect Number or not?


Program should accept a number using C++ and find out it is Perfect Number or not.

What are Perfect Numbers?

Perfect Numbers are those numbers who's sum of individual factors is same as the number itself, with an  exception the number itself should not be included as one of the factors.

C++ Solution
(Keeping in mind the students of class XI/XII having Computer Science with C++ as one of their subject)

#include<iostream.h>
#include<conio.h>
int chk_PerfectNumber(int n)
{
int sum=0;
for(int i = 1; i < n; i++)
        {
            if(n % i == 0)
            {
                sum = sum + i;
            }
        }
        if(sum == n)
        {
            return 1;
        }
        else
        {
            return 0;
        }
}

void main()
{
         clrscr();
         int n;
         cout<<"\nEnter the number to find it is Perfect Number or not?";
         cin>>n;
         int chk=chk_PerfectNumber(n);
         if(chk==1)
         {
                  cout<<"\n It is a Perfect Number";
         }
         else
         {
                  cout<<"\n It is not a Perfect Number";
          }
}


Comments

Popular posts from this blog

Online Classes in Computer Science (Python) from Edukers

CS - Program to generate Fibonacci Series