Previously we Have Discussed the Program for
Here we learn the logic to test weather the given number or int is perfect or not.
A perfect number is a positive integer that is equal to the sum of its positive divisors, that is, the sum of its positive divisors excluding the number itself.
For Example The first perfect number is 6, because 1, 2, and 3 are its positive divisors, and sum of 1 + 2 + 3 = 6. Other Perfect Numbers are 28 ,496 and so on.
public class PerfectNumber {
public static boolean isNumPerfect(int num){
boolean status = false;
int temp = 0;
for(int i=1;i<=num/2;i++){
if(num%i == 0){
temp+= i;
}
}
if(temp == num){
status = true;
} else{
status = false;
}
return status;
}
public static void main(String[] args) {
System.out.println("Is 6 Perfect Number : "+PerfectNumber.isNumPerfect(6));
System.out.println("Is 28 Perfect Number : "+PerfectNumber.isNumPerfect(28));
System.out.println("Is 30 Perfect Number : "+PerfectNumber.isNumPerfect(30));
}
}
Program Output:
Is 6 Perfect Number : true
Is 28 Perfect Number : true
Is 30 Perfect Number : false
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Java I/O Tutorial

No comments:
Post a Comment