C Programming Code Examples C > For Loops and While Loops Code Examples program to find perfect numbers between 1 to n program to find perfect numbers between 1 to n Write a C program to find all Perfect numbers between 1 to n. C program to find all perfect numbers between given range. How to generate all perfect numbers between given interval using loop in C programming. What is Perfect number? Perfect number is a positive integer which is equal to the sum of its proper positive divisors. For example: 6 is the first perfect number Proper divisors of 6 are 1, 2, 3 Sum of its proper divisors = 1 + 2 + 3 = 6. Hence 6 is a perfect number. Logic to find all Perfect number between 1 to n Input upper limit from user to find Perfect numbers. Store it in a variable say end. Run a loop from 1 to end, increment 1 in each iteration. The loop structure should look like for(x=1; x<=end; x++). For each iteration inside loop print the value of x if it is a Perfect number. #include <stdio.h> int main() { int x, j, end, sum; /* Input upper limit to print perfect number */ printf("Enter upper limit: "); scanf("%d", &end); printf("All Perfect numbers between 1 to %d:\n", end); /* Iterate from 1 to end */ for(x=1; x<=end; x++) { sum = 0; /* Check whether the current number x is Perfect number or not */ for(j=1; j<x; j++) { if(x % j == 0) { sum += j; } } /* If the current number x is Perfect number */ if(sum == x) { printf("%d, ", x); } } return 0; }