Perfect Number C Program

By | November 17, 2015

C Program To Find Perfect Number

Learn How To Find Perfect Number in C Programming Language. It is important that we should know How A For Loop Works before getting further with the C Program Code. You will have to make use of Modulus Operator to Find Numbers that are Perfect in C Code.

What is a Perfect Number?

It is a Positive Integer (or Number) which is equal to the Sum of their proper Positive Divisors, excluding the Number itself.

Example

6, 496, 28, 8128

Must Read: C Program To Check if a Number is a Perfect Square Number or Not

C Program For Perfect Number using While Loop

#include<stdio.h>

int main()
{
      int num, sum = 0, count = 1;
      printf("Enter A Number:\t");
      scanf("%d", &num);
      while(count < num)
      {
            if(num%count == 0)
            {
                  sum = sum + count;
            }
            count++;
      }
      if(sum == num)
            printf("%d is a Perfect Number", num);
      else
            printf("%d is not a Perfect Integer", num);
      printf("\n");
      return 0;
}

Must Read: C Program To Find Quadratic Roots of an Equation

C Program To Find Perfect Number using For Loop

#include<stdio.h>

int main()
{
      int num, sum = 0, count;
      printf("Enter A Number:\t");
      scanf("%d", &num);
      for(count = 1; count < num; count++)
      {
            if(num%count == 0)
            {
                  sum = sum + count;
            }
      }
      if(sum == num)
            printf("%d is a Perfect Number", num);
      else
            printf("%d is not a Perfect Integer", num);
      printf("\n");
      return 0;
}

Must Read: C Program To Calculate Greatest Common Divisor (GCD) of Two Numbers

 

Check Perfect Number using Function in C Programming

#include<stdio.h>

int check_perfect_number(int x)
{
      int sum = 0, count = 1;
      while(count < x)
      {
            if(x%count == 0)
            {
                  sum = sum + count;
            }
            count++;
      }
      return sum;
}

int main()
{
      int num, result;
      printf("Enter A Number:\t");
      scanf("%d", &num);
      result = check_perfect_number(num);	
      if(result == num)
            printf("%d is a Perfect Number", num);
      else
            printf("%d is not a Perfect Integer", num);
      printf("\n");
      return 0;
}

Must Read: C Program To Find Sum of Digits of an Integer

 

Output

Find Perfect Number in C Programming Language

If you have any compilation errors or doubts in this C Program To Find Perfect Numbers, let us know about in the Comment Section below.

2 thoughts on “Perfect Number C Program

  1. Sudhit Singh

    Thanks. I was looking for perfect number program in c using function. You have given so many ways to solve the perfect numbers.

    Reply
    1. Tushar Soni Post author

      We are glad that you liked the Perfect Number C Program. We have given 3 methods as we feel that it would help programming beginners to understand it in a much better way with comparison of different methods.

      Reply

Let's Discuss