What is a perfect number?
“Perfect number is a positive number which sum of all positive divisors excluding 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
and 28 is also a Perfect Number
since 1+ 2 + 4 + 7 + 14= 28
Other perfect numbers: 496, 8128

C Code

#include <stdio.h>
#include <conio.h>

void main()                 //Start of main
{
	int i=1, sum=0, num;		//Variable Initializing 
	printf("Enter Number: ");
	scanf("%d", num);
	while(i<num)
	{                                  // start of loop.
		if(i < num)
		{
			if(num%i==0 )
			sum=sum+i;
		}                          //End of if statement
		i++;
	}

	   if(sum==num)			//Checking if sum equals to original number
	   {
		printf("%d is a perfect number.\n", num);
	   }
	   else
	   {
		printf("%d is not a perfect number.\n", num);
	   }
                             //End of loop
	  getch();
}                            //End of main

C++ Code

#include <iostream>
#include <conio.h>

int main()                 //Start of main
{
	using namespace std;
	int i=1, sum=0, num;		//Variable Initializing 
	cout << "Enter Number: "; cin >> num;
	while(i<num)
	{                                  // start of loop.
		if(i < num)
		{
			if(num%i==0 )
			sum=sum+i;
		}                          //End of if statement
		i++;
	}

	   if(sum==num)			//Checking if sum equals to original number
	   {
		cout<<num<<" is a perfect number."<<"\n";
	   }
	   else
	   {
		cout<<num<<" is not a perfect number."<<"\n";
	   }
                             //End of loop
	  getch();
}                            //End of main

Output:

Other Related Search Terms

  1. Code For Perfect Number In C++
  2. Code For Perfect Number In C
  3. C++ Programs to find perfect number
  4. C Programs to find perfect number
  5. Find all perfect numbers
Share.

I'm a full-stack developer, specializing in the PHP, JS, Wordpress, MEAN Stack, MERN Stack & Django. I hold a high standard of quality in everything that I do.

Leave A Reply