C program to check if number is perfect no. or not

Perfect Number Definition – Perfect number is a positive integer which is equal to the sum of it’s divisiors. Ex – 6 = 1+2+3. Therefore, 6 is a perfect number.

Below is the C program in the C programming language to check if the inputted number is a perfect number or not –

#include<stdio.h>
void main()
{
     int n,i=1,sum=0;
     
     printf("Enter a number: ");
     scanf("%d",&n);
     while(i<n)
     {
               if(n%i==0)
               sum=sum+i;
               i++;
     }
     
     if(sum==n)
     {
               printf("%d is a perfect number",i);
     }
     else
     {
         printf("%d is not a perfect number",i);
     }
     getch();
}

C program to Print the series of all the perfect no. up to the desired range

Below is the solution of the C Program –

#include<stdio.h>
void main()
{
     int n,i,sum,min,max;

  printf("Enter the minimum range: ");
  scanf("%d",&min);

  printf("Enter the maximum range: ");
  scanf("%d",&max);

  printf("Perfect numbers in given range is: ");
  for(n=min;n<=max;n++)
  {
        i=1;
        sum = 0;
        while(i<n)
        {
                  if(n%i==0)
                  sum=sum+i;
                  i++;
        }
        if(sum==n)
        printf("%d ",n);
  }
  getch();
}

yashan has written 70 articles

2 thoughts on “C program to check if number is perfect no. or not

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>