C Programming Code Examples C > Functions Code Examples C program to print perfect numbers between given interval using function C program to print 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. Declare function to print all perfect numbers in given range First give meaningful name to the function. Say printPerfect() will print all perfect numbers in given range. Along with this declare and define one more function int isPerfect(int x); to check perfect number. Next the function must accept two parameters i.e. start and end limit to print perfect numbers in range. Hence, the function declaration should look like printPerfect(int start, int end); Finally the function prints all perfect numbers in given range returning nothing. Hence, return type of the function must be void. Final function declaration to print all perfect numbers in given range is - void printPerfect(int start, int end); #include <stdio.h> /* Function declarations */ int isPerfect(int x); void printPerfect(int start, int end); int main() { int start, end; /* Input 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; } /** * Check whether the given number is perfect or not. * Returns 1 if the number is perfect otherwise 0. */ int isPerfect(int x) { int j, sum; /* Finds sum of all proper divisors */ sum = 0; for(j=1; j<x; j++) { if(x % j == 0) { sum += j; } } /* * If sum of proper positive divisors equals to given number * then the number is perfect number */ if(sum == x) return 1; else return 0; } /* Print 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++; } }