Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Jul 9, 2024
Easy

Perfect Number Program in C

Table of contents
Learn to use AI Tools & ChatGPT to excel as Microsoft SDE
11 Jul, 2024 @ 01:30 PM
Speaker
Pranav Malik
SDE 2 @

Introduction

A perfect number in C is a positive integer that equals the sum of its proper divisors, excluding itself. To determine if a number is perfect, iterate through its divisors and sum them. If the sum equals the number itself, it's perfect. Examples include 6 (1+2+3=6) and 28 (1+2+4+7+14=28).

In this blog we will discuss the approach to determine whether or not a given integer is perfect.

 

Also Read, Binary to Hex Converter and C Static Function.

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

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Approach to Find 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;
}

 

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

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

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 articles on 

 

Refer to our guided paths on Code360 to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available, Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Happy Learning!