Perfect Number Program in C

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest in 3 Numbers
Find Grade of student
Find the absolute value
Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

In this post, we will write the Perfect number program in C. We will also write a program to check the perfect number in a given range.

Perfect number:- A number whose factors sum, except itself equals the same number.

Example:- 6
Factors of 6 (except itself) are 1,2,3.
The Sum of these factors 1+2+3 = 6 So, 6 is a Perfect number.

Another example:- 28
Factors of 28 (except itself) are 1, 2, 4, 7, 14
The sum of factors of 28 = 1+2+4+7+14 = 28
Hence, 28 is also a perfect number.

The perfect number program is based on finding the factors of a number. If you don’t know how to find factors of a number using the C program then learn it first.

C program to Check Perfect Number

 #include<stdio.h>
 int main()
 {
     int num, sum=0;

     printf("Enter Number: ");
     scanf("%d",&num);

     for(int i=1; i<=num/2; i++)
     {
         if(num%i==0)
             sum+=i;
     }

     if( sum==num )
         printf("%d is a perfect number.\n",num);
     else
         printf("%d is not a perfect number.\n",num);

     return 0;
 }

Output:-

Enter Number: 6
6 is a perfect number.

Enter Number: 10
10 is not a perfect number.

Enter Number: 28
28 is a perfect number.

The same problem also can be done using while loop.

C program to check Perfect Number in a given Range

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

     printf("Enter min and max value of the range: ");
     scanf("%d %d", &min, &max);

     printf("Perfect numbers in the given range are: \n");

     for(int i=min; i<=max; i++)
     {
         sum=0;
         for(int j=1; j<=i/2; j++)
         {
             if(i%j==0)
                 sum+=j;
         }
         if(i==sum) printf("%d\t",i);
     }

     return 0;
 }

Output:-

Enter min and max value of the range: 1 100
Perfect numbers in the given range are:
6 28

Enter min and max value of the range: 100 10000
Perfect numbers in the given range are:
496 8128

C program to find the perfect number using the Function

A function is a block of code that performs a specific task. For example, the main is a function and every program execution starts from the main function in C programming.

The function is a small program is used to do a particular task. In C a big program divided into several small subroutines/functions/procedures. Hence C is a function-oriented programming language.

Function to check perfect number

 int checkPerfect(int number)
 {
   int sum = 0;
   for(int i=1;i<=number/2;i++)
   {
       if(number%i==0)
           sum += i;
   }
   if( sum == number ) return 0;
   else return 1;
 }

Below program checks, the given number is a perfect number or not.

 #include<stdio.h>
 int checkPerfect(int number)
 {
   int sum = 0;

   for(int i=1;i<=number/2;i++)
   {
       if(number%i==0)
           sum += i;
   }

   if( sum == number ) return 0;

   else return 1;
 }

 int main()
 {
     int number, sum=0;

     printf("Enter Number: ");
     scanf("%d",&number);

     if(checkPerfect(number) == 0)
     printf("%d is a perfect number.\n",number);
     else
     printf("%d is not a perfect number.\n",number);

     return 0;
 }

Output:-

Enter Number: 496
496 is a perfect number.

Enter Number: 500
500 is not a perfect number.

Using the checkPerfect() function we can also write a C program to find perfect number in a given range, try it yourself.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest among 3 Numbers
Find Grade of student
Find the absolute value
Check Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

Leave a Reply