Please click on 1-2 Ads to keep this website's Study Material FREE and regularly Updated.

Program 46: Write a C++ program to print all the perfect numbers lying in the given range.

#include<iostream>
using namespace std;

void perfect(int start,int end)
{
	for(int i=start;i<=end;i++)
	{
		int sum=0;
		for(int j=1;j<=i/2;j++)
		{
			if(i%j==0)
			sum+=j;
		}
		if(sum==i)
		{
			cout<<i<<" ";
		}
	}
}

int main()
{
	int start,end;
	cout<<"\nenter the range : ";
	cin>>start>>end;
	cout<<"\nperfect numbers in a given range :\n";
	perfect(start,end);
	return 0;
}


If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.