Tuesday, 5 September 2017

C-Program to Print all Perfect number between 1 and given number



#include<stdio.h>
#include<math.h>

int 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);
    }
  }
return 0;
}

No comments:

Post a Comment