Find Perfect Numbers From Given Range by using Java Program
Code:
import java.util.*;
public class PerfectNumberRange
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter The Starting Number : ");
int m = sc.nextInt();
System.out.print("\nEnter The Ending Number : ");
int n = sc.nextInt();
System.out.println("\nPerfect Numbers : ");
for(int i=m;i<=n;i++)
{
int c = 0;
for(int j=1;j<i;j++)
{
if(i%j==0)
{
c = c+j;
}
else
{
c = c;
}
}
if(c==i)
{
System.out.println("\n"+i);
}
}
System.out.println();
}
}
Output of program:
import java.util.*;
public class PerfectNumberRange
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter The Starting Number : ");
int m = sc.nextInt();
System.out.print("\nEnter The Ending Number : ");
int n = sc.nextInt();
System.out.println("\nPerfect Numbers : ");
for(int i=m;i<=n;i++)
{
int c = 0;
for(int j=1;j<i;j++)
{
if(i%j==0)
{
c = c+j;
}
else
{
c = c;
}
}
if(c==i)
{
System.out.println("\n"+i);
}
}
System.out.println();
}
}
Output of program:
No comments