Table of contents
1.
Introduction
2.
What is Perfect Number?
3.
Approach to Find the Perfect Number
4.
C Program to Check Perfect Number or Not
4.1.
Method 1 - Using for Loop
4.2.
C
4.3.
Method 2 - Using While Loop
4.4.
C
4.5.
Method 3 - Using Recursion
4.6.
C
5.
Frequently Asked Questions
5.1.
How can you figure out if a number is perfect?
5.2.
What are the perfect numbers from 1 to 100?
5.3.
How does the C program determine if a given number is perfect?
5.4.
What factors are used in calculating Armstrong?
5.5.
In the C programming language, which loop is the fastest?
6.
Conclusion
Last Updated: Jan 5, 2025
Easy

Perfect Number Program in C

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A Perfect Number is a positive integer equal to the sum of its proper divisors, excluding itself. For example, 6 and 28 are perfect numbers. Writing a program in C to find perfect numbers involves using loops and conditional statements to calculate divisors and verify the condition, making it an excellent exercise for beginners in programming.

Perfect Number Program in C

 

Also Read, Binary to Hex Converter

What is Perfect Number?

A perfect number is a positive integer whose sum of its proper positive divisors equal to the perfect number.

For instance, The first perfect number is 6.

1, 2, and 3 are the proper divisors of 6.

1 + 2 + 3 = 6 is the sum of the correct divisors.

As a result, the number 6 is perfect.

The number 28 is the second perfect number.

28's proper divisors are 1, 2, 4, 7, and 14.

1 + 2 + 4 +7 +14 = 28 is the sum of its proper divisors.

As a result, 28 is a perfect number.

You can also read about dynamic array in c and  Tribonacci Series

Approach to Find the Perfect Number

Let's look at step-by-step descriptive logic to find the perfect number.

  1. Take a number from the user and input it. Put it in a variable called Pnum.
  2. Set another variable to 0 to store the sum of proper positive divisors.
  3. In each iteration of a loop from 1 to Pnum/2, increment 1 by one. The loop should be structured as for(i=1; i=Pnum/2; i++).

4. If the current number, i is a valid positive divisor of num, add it to the sum inside the loop.

5. Finally, see if the original number is equal to the sum of positive divisors. Otherwise, the given integer is not a Perfect number.

C Program to Check Perfect Number or Not

You can find the perfect number in C using three different methods:

  1. Using for Loop
  2. Using while Loop
  3. Using recursion in C

Method 1 - Using for Loop

  • C

C

#include <stdio.h>

int isPerfect(int num);

int main() {
int num;

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

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

return 0;
}

int isPerfect(int num) {
int sum = 0;
for (int i = 1; i < num; i++) {
if (num % i == 0) {
sum += i;
}
}
return sum == num;
}
You can also try this code with Online C Compiler
Run Code

 

Output:

Enter a number: 28
28 is a perfect number.

 

Explanation:

  • The program prompts the user to input a number.
  • The isPerfect() function checks if the given number is perfect or not.
  • It iterates through numbers from 1 to num - 1, summing up the divisors of num.
  • If the sum equals num, the function returns true, indicating that num is a perfect number, else it returns false.

Method 2 - Using While Loop

  • C

C

#include <stdio.h>

int isPerfect(int num);

int main() {
int num;

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

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

return 0;
}

int isPerfect(int num) {
int sum = 0;
int i = 1;
while (i < num) {
if (num % i == 0) {
sum += i;
}
i++;
}
return sum == num;
}
You can also try this code with Online C Compiler
Run Code

 

Output:

Enter a number: 28
28 is a perfect number.

 

Explanation:

  • This method uses a while loop to iterate through numbers from 1 to num - 1.
  • The isPerfect() function calculates the sum of divisors similar to Method 1.
  • It checks if the sum equals the number itself to determine if it's perfect.

Method 3 - Using Recursion

  • C

C

#include <stdio.h>

int isPerfect(int num, int i, int sum);

int main() {
int num;

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

if (isPerfect(num, 1, 0))
printf("%d is a perfect number.\n", num);
else
printf("%d is not a perfect number.\n", num);

return 0;
}

int isPerfect(int num, int i, int sum) {
if (i == num)
return sum == num;
if (num % i == 0)
sum += i;
return isPerfect(num, i + 1, sum);
}
You can also try this code with Online C Compiler
Run Code

 

Output:

Enter a number: 28
28 is a perfect number.

 

Explanation:

  • This method uses recursion to iterate through numbers from 1 to num - 1.
  • The isPerfect() function is called recursively.
  • It calculates the sum of divisors and checks if the sum equals the number itself to determine if it's perfect.

Frequently Asked Questions

How can you figure out if a number is perfect?

A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself, according to number theory. 6 has divisors of 1, 2, and 3 (excluding itself), and 1 + 2 + 3 = 6, making it a perfect number.

What are the perfect numbers from 1 to 100?

Perfect numbers from 1 to 100 are 6 and 28. These numbers are divisible by their proper divisors, excluding themselves. For example, 6 equals the sum of its divisors (1, 2, 3), and 28 equals the sum of its divisors (1, 2, 4, 7, 14).

How does the C program determine if a given number is perfect?

In a C program, to determine if a given number is perfect, it iterates through its proper divisors (excluding itself) using a loop or recursion. It calculates the sum of these divisors and checks if the sum equals the original number, indicating that it's a perfect number.

What factors are used in calculating Armstrong?

If the total of the cubes of a number's digits equals the number itself, it is said to be an armstrong. Consider the two-digit integer xy; if x³ + y³ = xy, xy is an armstrong number. 371, for example, can be written as 3³ + 7³ + 1³ = 371 and is thus an armstrong number.

In the C programming language, which loop is the fastest?

In the C programming language, the for loop is often considered the fastest among the three main loop constructs (for, while, and do-while) when iterating over a known range or performing a fixed number of iterations. However, the performance difference between loop constructs is typically minimal and largely dependent on the specific use case and the compiler optimization.

Conclusion

In this article, we have discussed two examples of C Programming to check whether the given number is a Perfect Number or not. We hope that this article has helped you enhance your knowledge regarding finding the perfect number. If you would like to learn more, check out our other articles:

Live masterclass