Program to check given input is a perfect number or not.
#include<iostream.h>
#include<conio.h>
void main()
{
int num,sum=0,i;
cout<<"Enter a number to check perfect or not : ";
cin>>num;
for(i=1;i<num;i++)
{
if(num%i==0)
{ sum+=i; }
}
if(num==sum)
{ cout<<endl<<num<<" is a perfect number."; }
else
{ cout<<endl<<num<<" is not a perfect number."; }
getch();
}
OR
#include<iostream.h>
#include<conio.h>
bool perfect(int a)
{
int sum=0;
for(int i=1;i<a;i++)
{
if(a%i==0)
{
sum+=i;
}
}
if(sum==a)
{return(1);}
else
{return(0);}
}
void main()
{
int num;
cout<<"Enter a number to check is it Perfect or not :";
cin>>num;
if(perfect(num)==1)
{
cout<<"\nEntered Number is a Perfect Number";
}
else
{
cout<<"\nEntered Number is not a Perfect Number";
}
getch();
}
No comments:
Post a Comment