Find perfect numbers between an interval
C program to find perfect numbers within a given number of range.
A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself.
For example, num = 28
Divisors of 28: 1, 2, 4, 7, 14, 28
Sum of divisors (excluding 28): 1 + 2 + 4 + 7 + 14 = 28
Sum = Original number
So, 28 is a perfect number
Some of the perfect numbers are 6, 28, 496, 8128, and 33550336, etc.
For example, num = 28
Divisors of 28: 1, 2, 4, 7, 14, 28
Sum of divisors (excluding 28): 1 + 2 + 4 + 7 + 14 = 28
Sum = Original number
So, 28 is a perfect number
Some of the perfect numbers are 6, 28, 496, 8128, and 33550336, etc.
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, sum;
int low, high;
printf("Enter two values of an interval\t:");
scanf("%d%d",&low,&high);
printf("Perfect numbers between %d and %d are: ", low, high);
for(i=low;i<=high;i++)
{
sum = 0;
for(j=1;j<i;j++)
{
if(i%j==0)
{
sum = sum + j;
}
}
if(sum == i)
printf("%d, ",i);
}
getch();
}
Output
Enter two values of an interval :2
10000
Perfect numbers between 2 and 10000 are: 6, 28, 496, 8128,