C Program check Perfect numbers between 1 and given number

#include<stdio.h>
#include<math.h>
main()
{
  int num,i,sum,j;
  printf("Enter a number to know all perfect numbers till n\n");
  scanf("%d",&num);
  printf("The perfect numbers between 1 and %d are:",num);
  for(i=1;i<=num;i++)
  {
    sum=0;
    for(j=1;j<i;j++)
    {
      if(i%j==0)
      {
        sum+=j;
      }
    }
    if(sum==i)
    {
      printf("%d\n",i);
    }
  }
}
Output:-

Enter a number to know all perfect numbers till n
1000
The perfect numbers between 1 and 1000 are:6
28
496

Post a Comment

If you have any doubts, Please let me know
Thanks!

Previous Post Next Post