Perfect Number Program In C - C Programming Tutorial
C Course / Programs / Perfect Number Program In C

Perfect Number Program In C

BLUF: Understanding Perfect Number Program In C is a foundational part of learning C programming. This tutorial explains the core principles and syntax needed to implement this concept effectively.
Core Programming Principle: Perfect Number Program In C

C provides direct access to memory and system resources. Learn how Perfect Number Program In C leverages this power in the lesson below.

For instance, the number 6 exemplifies a positive integer that can be evenly divided by 1, 2, and 3. It is noteworthy that a number is always divisible by itself, a fact we account for when summing up the divisors. The total of these divisors (1 + 2 + 3 = 6) amounts to the number itself, affirming that 6 qualifies as a perfect number.

There exist two methods to discover the ideal number:

  • Leveraging the for Loop
  • Employing the while Loop
  • Using for Loop

Create a C program that prompts the user for an input and verifies if the provided number is a perfect number or not.

Example

/*C program to check whether the given number is the Perfect number*/
#include<stdio.h>
#include<conio.h>
void main()
{
// declare and initialize the variables
int num, rem, sum = 0, i;
// take an input from the user.
printf("Enter a number\n");
scanf("%d", &num);    
// find all divisors and add them
for(i = 1; i < num; i++)
                     {
                              rem = num % i;
                             if (rem == 0)
                                        {
                                               sum = sum + i;
                                         }
                        }
if (sum == num)
                      printf(" %d is a Perfect Number");
           else
                      printf("\n %d is not a Perfect Number");
getch();
}

Output

Output

Enter a number
 [value] is a Perfect Number
 [value] is not a Perfect Number

In the preceding output, the loop condition is checked at every iteration, and the counter i is increased by 1. Within the loop, different tasks are executed including:

Begin with setting the variable i to 1, then calculate the remainder by dividing num by i, which results in 28 % 1 = 0. This means that the remainder is equal to 0.

Step 2: rem == 0, condition true.

Step 3: sum = 0 + i, sum = 0 + 1 => 1

// i is incremented by 1

At Step 4, when i is equal to 2 and rem is calculated as the remainder of dividing num by i, which results in 28 % 2 equal to 0. In this case, rem is not equal to 0, therefore the condition evaluates to true.

Sum = 1 + i => 1 +2 = 3

In step 5, when i equals 3 and rem is calculated as the remainder of dividing num by i (i.e., 28 % 3 = 1), the condition evaluates to false since rem is 1, not 0.

Step 6: Assign the value 4 to the variable i and calculate the remainder of dividing num by i, which results in 28 % 4 = 0. In this case, the condition rem == 0 evaluates to true.

Sum = 1 + i => 3 + 4 = 7

Similarly, check all condition;

Step 7: When the sum is equal to the given number, such as 28 being equal to 28, display the following message: "The provided number is a Perfect Number".

Using while Loop

Example 2: We will generate a C Program to identify the perfect number by employing a while loop.

Example

/*Create a C Program to find the perfect number using while loop*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i = 1, num, Sum = 0;
printf(" Enter any number to check Perfect Number \n");
scanf("%d", &num);

while(i < num )
                     {
                               if(num % i == 0)
                               Sum = Sum + i;
                               i++;
                     }
           if(Sum == num)
                  printf("\n %d is Perfect Number", num);
           else
           printf("\n %d is not a Perfect Number", num);
getch();
}

Output

Output

Enter a number
 [value] is a Perfect Number
 [value] is not a Perfect Number

Example 3: Find the perfect number between two numbers through a C program.

Example

#include<stdio.h>
#include<conio.h>
void main()
{
int num, Sum = 0, i, Max, Min;
// Take two number from the user: min and max
printf("Enter the minimum and the maximum value\n");
scanf("%d %d", &Min, &Max);

printf("\n Perfect Number between %d and %d are\n ", Min, Max);
for(num = Min; num <= Max; num++)
                     {
                             for(i = 1, Sum = 0; i < num; i++)
                                     {
                                            if(num %i == 0)
                                            Sum = Sum + i;
                                      }
                              if(Sum == num)
                             printf("%d \t", num);
                      }
  getch();
}

Output

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience