Perfect Numbers
PROBLEM :
Given a number and check if a number is perfect or not. A number is said to be perfect if sum of all its factors excluding the number itself is equal to the number.
Input:
First line consists of T test cases. Then T test cases follow .Each test case consists of a number N.
Output:
Output in a single line 1 if a number is a perfect number else print 0.
Constraints:
1<=T<=300
1<=N<=10000
Example:
Input:
2
6
21
Output:
1
0
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<math.h>
bool Perfect_Numbers(int ) ;
int main()
{
int t,no ;
cin>>t ;
while(t--)
{
cin>>no ;
if(Perfect_Numbers(no))
cout<<1 ;
else
cout<<0 ;
cout<<endl ;
}
return 0;
}
bool Perfect_Numbers(int no)
{
int sum,i,s ;
sum=0 ;
if(no==1||no==0)
return false ;
for(i=2;i<=sqrt(no);i++)
if(no%i==0)
{
sum+=i ;
sum+=no/i ;
}
if(sum+1==no)
return true ;
return false ;
}
---------------------------------------------------------------------------------
Given a number and check if a number is perfect or not. A number is said to be perfect if sum of all its factors excluding the number itself is equal to the number.
Input:
First line consists of T test cases. Then T test cases follow .Each test case consists of a number N.
Output:
Output in a single line 1 if a number is a perfect number else print 0.
Constraints:
1<=T<=300
1<=N<=10000
Example:
Input:
2
6
21
Output:
1
0
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<math.h>
bool Perfect_Numbers(int ) ;
int main()
{
int t,no ;
cin>>t ;
while(t--)
{
cin>>no ;
if(Perfect_Numbers(no))
cout<<1 ;
else
cout<<0 ;
cout<<endl ;
}
return 0;
}
bool Perfect_Numbers(int no)
{
int sum,i,s ;
sum=0 ;
if(no==1||no==0)
return false ;
for(i=2;i<=sqrt(no);i++)
if(no%i==0)
{
sum+=i ;
sum+=no/i ;
}
if(sum+1==no)
return true ;
return false ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment