A perfect number in mathematics is a positive integer that is equal to the total of its positive divisors, excluding the original number.
For instance, the positive integer 6 is evenly divided by the numbers 1, 2, and 3. Although we are aware that the number is also divisible on its own, we will nonetheless add it to the list of divisors. These divisors (1 + 2 + 3 = 6) when added together result in 6, which is the same as the number we have been considering. As a result, 6 is a perfect number.
What is the Perfect Number?
The sum of a perfect number’s appropriate positive divisors, except the number itself, is a positive integer. Here’s an illustration,
Input: n = 6
Proper divisors of 6 are 1, 2, 3
Sum of its proper divisors = 1 + 2 + 3 = 6
Hence 6 is a perfect number.
Algorithm to Find Perfect Number
- Request an integer from the user.
- To hold the total of the correct positive divisors, initialize another variable with 0.
- From 1 through n/2, go over each integer and see if it is a divisor. Keep track of the total of all divisors. The number will be a perfect number if the total equals n; else, it will not be a perfect number.
Program to Find Perfect Number in C
There are three ways to find the perfect number in C:
- Using for Loop
- Using while Loop
- Using recursion in C
Method 1: Using for Loop
In this method, we will write a c program that accepts an integer input from the user and checks whether the given number is a perfect number or not.
#include <stdio.h> int main () { int num = 28, sum = 0; // iteratively check for all numbers in range [1, 27] for(int i = 1; i < num; i++){ // check if i is a divisor, if yes then add to sum if(num % i == 0) sum = sum + i; } if(sum == num) printf("%d is a perfect number",num); else printf("%d is not a perfect number",num); }
Explanation: In the given example, Every value of i is used to run the loop. If the computed remainder equals 0, the total is equal to the sum plus j. A condition outside of the loop determines if the total is equal to the number, and if it is, the print statement is executed.
Method 2: Using a while loop
In this method, we will use a while loop to check whether the given number is a perfect number or not.
#include <stdio.h> int main () { int num = 28, sum = 0, i = 1; // iteratively check for all numbers if they are divisors while(i < num) { // check if i is a divisor, if yes then add to sum if(num % i == 0) sum = sum + i; i++; } if(sum == num) printf("%d is a perfect number",num); else printf("%d is not a perfect number",num); }
Explanation: In the given example, Every value of i is iterated via the while loop. A total is determined. A condition outside of the loop determines if the total is equal to the number, and if it is, the print statement is executed.
Method 3: Using recursion
In this method, we will use a recursive method to check whether the given number is a perfect number or not.
#include <stdio.h> static int sum = 0; int getSumDivisors(long num, int i) { // since, all factors can be found will num/2 // we will only check in this range if(i <= num/2) { if(num % i ==0) sum = sum + i; i++; // recursively call isPerfect getSumDivisors(num, i); } //returns the sum // when i becomes > num/2 return sum; } int main () { int num = 28; if(getSumDivisors(num, 1) == num) printf("%d is a perfect number",num); else printf("%d is not a perfect number",num); return 0; }
Explanation: In the given example, the function, PerfectNum does the calculation and returns the sum. Out of the loop, there is a condition that checks whether or not the sum is equal to the number, and accordingly, the print statement is executed.
Other C Programs
C Program for Binary Search
C Program to Add Two Numbers
C Program to Calculate Percentage of 5 Subjects
C Program to Convert Binary Number to Decimal Number
C Program to Convert Celsius to Fahrenheit
C Program to Convert Infix to Postfix
C Program to Find Area of Circle
C Program to Find Roots of Quadratic Equation
C program to Reverse a Linked List
C program to reverse a number
Ascending Order Program in C
Menu Driven Program For All Operations On Doubly Linked List in C
C Program for Armstrong Number
C Program For Merge Sort For Linked Lists
C program for performing Bubble sort on Linked List
Hello World Program in C
Leap Year Program in C
Odd Even Program in C
Selection Sort Program in C