Contact Learn C
Copy

Program 30:To Print all Perfect number between 1 and given number.

Program 30:                                                                                              What is Perfect 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);
    }
  }
}
Explanation:
This Program is same as the previous program where we found whether the given number is Perfect or not. The only change here is we use forloop from 1 to the given number(say 1000 in our case) and check each number whether it is perfect or not. So it checks from 1,2,3,...1000 and if any number is having the sum of it's divisors equal to the number then it is printed else not.

 Output:

Print all Perfect number between 1 and given number.
 
Donate

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide