Given a starting number and the ending number the task is to find the all the Perfect Number present in that given range and also to display it’s frequency.
Perfect Number is a number which is equal to sum of it’s proper divisors.
Example 1: 6 has proper divisors 1,2,3. 1+2+3 = 6. Hence it is a Perfect Number.
Example 2: 10 has proper divisors 1,2,5. 1+2+5 != 6. Hence it is not a Perfect Number.
Java
import java.util.*;
public class PerfectNumberRange
{
public static void main(String args[])
{
int start=0,end=0,sum=0,frequency=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a starting range: ");
start=sc.nextInt();
System.out.print("Enter a ending range: ");
end=sc.nextInt();
System.out.println("Perfect Numbers in the given range are:-");
for(int i=start;i<=end;i++)
{
for(int j=1;j<i;j++)
{
if(i%j==0)
{
sum=sum+j;
}
}
if(sum==i)
{
System.out.println(i);
frequency++;
}
sum=0;
}
System.out.println("Frequency: "+frequency);
}
}Java