//Write a program to check number is perfect or not?
A perfect number is a positive integer that is equal to the sum of its proper positive divisors,
that is, the sum of its positive divisors excluding the number itself. Equivalently, a perfect number
is a number that is half the sum of all of its positive divisors. The first perfect number is 6,
because 1, 2 and 3 are its proper positive divisors, and 1 + 2 + 3 = 6. Equivalently,
the number 6 is equal to half the sum of all its positive divisors:
( 1 + 2 + 3 + 6 ) / 2 = 6.
import
java.util.Scanner;
public
class PerfectNumber {
public
static void
main(String[] args) {
int n,sum=0;
Scanner s1=new
Scanner(System.in);
System.out.println("enter
number");
n=s1.nextInt();
for(int
i=1;i<n;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}
if(sum==n)
{
System.out.println("Number
is perfect");
}
else
{
System.out.println("Number
is not perfect");
}
}
}
o/p:---------------
enter
number
6
Number is perfect
enter
number
28
Number is perfect
enter
number
45
Number is not perfect
package govind;
public
class PerfectNo {
public
String checkPerfect(int n)
{
int sum=0;
for(int
i=1;i<=n/2;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}
if(sum==n)
{
return "Number
is perfect";
}
else
{
return "Number
is not perfect";
}
}
public
static void
main(String[] args) {
PerfectNo pn=new
PerfectNo();
String s1=pn.checkPerfect(28);
System.out.println(s1);
}
}
o/p:---------
Number
is perfect
No comments:
Post a Comment