Skip to content

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

by yashan on February 5th, 2012

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();
}

One Comment
  1. Blue Moon permalink

    it was quite nie

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS