C program to print all perfect numbers between given interval using function

Write a function to print all perfect numbers in a given interval in C programming. How to print all perfect numbers in a given range using functions in C program. C program to print all perfect numbers between 1 to n using functions.

Example:
Input lower limit: 1
Input upper limit: 100
Output perfect numbers: 6, 28

Required knowledge

Basic C programming, Function

Logic to print perfect numbers using function

As we already seen in previous posts about what is Perfect numbers, how to check perfect number and how to print all perfect numbers in a given range using loop. Basically printing all perfect numbers between a given range includes following steps:
  1. Run a loop from 1 to n (where n is the number which is to be checked as perfect number).
  2. If the current number of loop counter clearly divides the number then add it to a variable sum.
  3. After the loop terminates check whether the sum equals to original number or not. If it does then it is a perfect number otherwise not.
To code the above steps into function we will define two functions namely isPerfect() and printPerfect() which will check whether the given number is perfect or not and print all perfect numbers in given range respectively.

Program to print perfect numbers using functions

/**
 * C program to print all perfect numbers in given range using function
 */
 
#include <stdio.h>


/* Function declarations */
int isPerfect(int num);
void printPerfect(int start, int end);



int main()
{
    int start, end;
    
    /* Reads lower and upper limit to print perfect numbers */
    printf("Enter lower limit to print perfect numbers: ");
    scanf("%d", &start);
    printf("Enter upper limit to print perfect numbers: ");
    scanf("%d", &end);
    
    printf("All perfect numbers between %d to %d are: \n", start, end);
    printPerfect(start, end);
    
    return 0;
}



/**
 * Checks whether the given number is perfect or not.
 * Returns 1 if the number is perfect otherwise 0.
 */
int isPerfect(int num)
{
    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;
}



/**
 * Prints all perfect numbers between given range start and end.
 */
void printPerfect(int start, int end)
{
    /* Iterates from start to end */
    while(start <= end)
    {
        if(isPerfect(start))
        {
            printf("%d, ", start);
        }
        
        start++;
    }   
}


Output
Enter lower limit to print perfect numbers: 1
Enter upper limit to print perfect numbers: 10000
All perfect numbers between 1 to 10000 are:
6, 28, 496, 8128,

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment