Perfect Number in C Programming Language
Perfect Number
“In mathematics, the perfect number is a positive integer that is the sum of its proper divisors, including one and excluding itself. For example, 6 is a perfect number. Divisors of 6(excluding 6) are 1,2,3, and their sum yields the number itself, i.e., 1+2+3=6.”
Perfect numbers have fascinated mathematicians for centuries. Ancient Greeks were the first to identify perfect numbers and study them. The first few examples of perfect numbers are 6, 28, 496, and 8128. Perfect numbers are rare and have so many interesting properties. All the known perfect numbers are even.
Finding the Perfect Number in C
We can find if the given number is perfect or not in programming. In programming, the way to check whether the given number is perfect is by using a loop and storing the sum of all the divisors in a variable. At last, we will check whether the sum of all the divisors except the number itself is equal to the number. If the number equals the sum, then the number is perfect. Otherwise, it is not. There are two approaches to achieving this. One uses a for loop, and the other uses a while loop.
Using a For Loop
Here we will take an input and check whether it is a perfect number. First, let us form an algorithm.
Algorithm:
- Declare the variables num, sum, i.
- Take input from the user and store it in num.
- Initialize sum to 0.
- Make a loop from i=1 to i<(num-1).
- Check if num%i is equal to 0.
- If yes, then add it to the sum variable.
- Exit the loop.
- Check if sum is equal to num.
- If yes, then print that number is perfect.
- If no, then print that number is not perfect.
- Exit
Now we will implement this algorithm in C language.
Code:
#include <stdio.h> int main() { int num, sum=0, i; printf("Enter a number: "); scanf("%d", &num); for(i=1; i<num; i++) { if(num%i == 0) { sum += i; } } if(sum == num) { printf("%d is a perfect number.", num); } else { printf("%d is not a perfect number.", num); } return 0; }
This C program checks whether the number entered by the user is perfect. If it is a perfect number, then the program says the given number is perfect. If it is not a perfect number, it says that the given number is imperfect.
Output:
Using While Loop
Now we will use another approach to check whether the given number is perfect. Let us first develop an algorithm for this approach.
Algorithm:
- Declare the variables num, sum, i.
- Take input from the user and store it in num.
- Initialize the num variable to 0.
- Initialize variable i to 1.
- 5. Start a while loop with the condition i<num.
- Check if num is divisible by i.
- If yes, add i to the sum variable.
- End if.
- Increment i by 1.
- End loop.
- Check if the sum variable is equal to the num variable.
- If yes, print that num is a perfect number.
- If no, print that num is not a perfect number.
- End if.
- Exit
Now we will implement this algorithm in C programming language.
Code:
#include <stdio.h> int main() { int num, sum=0, i=1; printf("Enter a number: "); scanf("%d", &num); while(i<num) { if(num%i == 0) { sum += i; } i++; } if(sum == num) { printf("%d is a perfect number.", num); } else { printf("%d is not a perfect number.", num); } return 0; }
This program also performs the same task. The only difference is here; we are using a while loop instead of for loop.
Output:
We have both approaches to check whether a given number is perfect.
Conclusion
Perfect numbers are an essential and fascinating concept of mathematics and computer science. Perfect numbers have interesting properties and have always been an area of research and study for mathematicians and computer scientists. Mathematicians and computer scientists can gain essential insights by understanding the basic concepts behind perfect numbers. All the perfect numbers are even.