C Program to Find Perfect Number Within a Given range
A number is called a perfect number if the sum of all its factors excluding the number itself is equal to the number. For example, consider the number 6. The factors of 6 are 1,2,3 and 6. Now, the sum of all its factors excluding the number itself is 1+2+3=6. Here, since the original number is equal to the sum of all its factors excluding the number itself, therefore it is a perfect number.
Here, in this program, we are given a range and our task is to find all the perfect numbers within that range. But before moving forward, if you are not familiar with the concept of loops in C, then do check the article on Loops in C.
Input: Enter the range: 1 100
Output: Perfect Numbers within that range are 6 28
There are two ways of solving this problem:
Method 1: Without using functions
Method 2: Using Functions
Let us take a look at each of these methods separately.
Program 1: Find the Perfect Number within a given Range
In this method, the perfect numbers within a given range are displayed. Here, in the main method itself, we declare a range and check for all the perfect numbers within that range. This is done by using two for loops and traversing through each element and checking for the sum of the factors for each element.
Algorithm:
- Start
- Declare two variables for the range.
- Initialize the two variables.
- Use two for loops to check whether a number is perfect or not.
- Use the first for loop to hold the elements. This for loop is also used to traverse through all the elements within the given range.
- The second for loop will iterate elements from 1 to that element and check for the sum of all its factor excluding the number itself.
- If the sum of all its factor excluding the number itself is equal to the number then it is a perfect number.
- All such elements which satisfy the above conditions are printed.
- Stop
Below is the code for the same.
The below program demonstrates how to print all the perfect numbers within a given range. This can be done by using two for loops. The first for loop will hold the element and the second for loop will iterate the elements from 1 to that element and check for the sum of all its factors. If the sum of all its factor is equal to the number itself then it is a perfect number.
// C program to print all Perfect numbers within a given range
#include <stdio.h>
int main()
{
int x, y, sum; //Declare variables
/* Input lower and upper limit from user */
printf("Enter the starting element of the range : ");
scanf("%d", &x); //Initialize the lower limit
printf("Enter the ending element of the range : ");
scanf("%d", &y); //Initialize the upper limit
printf("All Perfect numbers between %d to %d:\n", x, y);
/* Iterate within the range to check for all the perfect numbers */
for(int i=x; i<=y; i++)
{
sum = 0; //Initialize the sum to zero for each element
/* Check whether the current number i is Perfect number or not */
for(int j=1; j<i; j++)
{
if(i % j == 0)
{
sum += j;
}
}
/* If the sum of all its factor is equal to the number itself then it is a perfect number */
if(sum == i)
{
printf("%d ", i);
}
}
printf(" \n ");
return 0;
}
Enter the starting element of the range: 1
Enter the ending element of the range: 1000
All Perfect numbers between 1 to 1000:
6 28 496
Program 2: Find the Perfect Number within a given Range
In this method, the perfect numbers within a given range are displayed using functions. Here, we declare two functions. The first function will check whether the given number is perfect or not and the second function will print the numbers if they are perfect. The first function will use two for loops to check whether the number is perfect or not. And the second function will use a while loop to iterate through all the perfect numbers and will print them.
Algorithm:
- Start
- Declare two variables for the range.
- Initialize the two variables.
- Call the functions to check whether a number is perfect or not.
- Use two for loops for the same.
- Use the first for loop to hold the elements. This for loop is also used to traverse through all the elements within the given range.
- The second for loop will iterate elements from 1 to that element and check for the sum of all its factor excluding the number itself.
- If the sum of all its factor excluding the number itself is equal to the number then it is a perfect number.
- All such elements which satisfy the above conditions are printed.
- Stop
Below is the code for the same.
The below program demonstrates how to print all the perfect numbers within a given range using functions. Here, we will call two functions. The first function will check whether the given number is perfect or not and the second function will print the numbers if they are perfect. In order to check whether a given number is perfect or not two for loops are used. The first for loop will hold the element and the second for loop will iterate the elements from 1 to that element and check for the sum of all its factors. If the sum of all its factor is equal to the number itself then it is a perfect number.
/* C program to print all perfect numbers in a given range using function */
#include <stdio.h>
int checkPerfect(int num); // Function declarations
void printPerfectNumber(int x, int y); // Function declarations
int main()
{
int x, y; //Range Declaration
/* Input lower and upper limit to print perfect numbers */
printf("Enter lower limit of the range: \n");
scanf("%d", &x); //Range Initialization
printf("Enter upper limit of the range: \n");
scanf("%d", &y); //Range Initialization
printf("\nAll perfect numbers between %d to %d are: \n", x, y);
printPerfectNumber(x, y); //Function Call
return 0;
}
/*
Check whether the given number is perfect or not.
Returns 1 if the number is perfect otherwise 0.
*/
int checkPerfect(int num) //Function Definition
{
int i, sum;
/* Finds sum of all proper divisors */
sum = 0;
for(i=1; i<num; i++)
{
if(num % i == 0)
{
sum += i;
}
}
/* If sum of proper positive divisors equals to given number then the number is perfect number */
if(sum == num)
return 1;
else
return 0;
}
/* Print all the perfect numbers between the given range from starting to end */
void printPerfectNumber(int x, int y) //Function Definition
{
/* Iterates within the given range from starting to end */
while(x <= y)
{
if(checkPerfect(x)) //Function Call
{
printf("%d ", x);
}
x++; //Increment for each iteration
}
}
Enter lower limit of the range: 1
Enter upper limit of the range: 1000
All perfect numbers between 1 to 1000 are:
6 28 496