C Program to Generate Perfect Numbers in Given Minimum to Maximum Ranges

A perfect number is a positive integer number in which sum of all positive divisors excluding the number itself is equal to that number.

For example: 28 is perfect number since its divisors are 1, 2, 4, 7 and 14. Sum of divisors is: 1+2+4+7+14=28.

Other examples of perfect number are: 6, 496 etc.

In this C program, we are going to generate perfect numbers in ranges from mimimum to maximum, where values of minimum and maximum are given by user.

Program


#include<stdio.h>
#include<conio.h>

int main()
{
	 int minimum, maximum, sum, i, j;
	 clrscr();
	 printf("Enter minimum: ");
	 scanf("%d", &minimum);
	 printf("%Enter maximum: ");
	 scanf("%d", &maximum);
	
	 /* Generating Perfect Numbers */
	 for(i=minimum; i<=maximum; i++)
	 {
	
		  /* Finding Sum of All Divisors */
		  sum = 0;
		  for(j=1;j< i;j++)
		  {
			   if(i%j==0)
			   {
			    	sum = sum + j;
			   }
		  }
		
		  /* Making Decision and Printing */
		  if(sum == i)
		  {
		   	printf("%d\t", i);
		  }
	 }
	 getch();
	 return(0);
}

Output of the above program :

Enter minimum: 1 ↲
Enter maximum: 500 ↲
6   28    496

Note: ↲ indicates enter is pressed.