C Program to Find all Perfect Numbers Between 1 to N

In this C program, we will find all perfect numbers between 1 to N using for loop. A perfect number is a positive number, that is equal to sum of its positive divisor.

For Example : 6 is a perfect number as positive factors of 6 are 1, 2 and 3 whose sum is 6.

Required Knowledge


C program to print all perfect numbers between 1 to N using for loop

#include<stdio.h>
 
int main () {
    int num, i, divSum, counter;
 
    printf("Enter a number\n");
    scanf("%d", &num);
 
    for(counter = 1; counter <= num; counter++){
        /* Find all divisors of a number between 
        1 to num/2 and add it to divSum */
        divSum = 0;
        for (i = 1; i <= counter/2; i++) {
            if (counter % i == 0) {
                divSum += i;
            }
        }
     
        if (divSum == counter)
            printf("%d is Perfect Number\n", counter);
    }
    return 0;
}
Output
Enter a number
500
6 is Perfect Number
28 is Perfect Number
496 is Perfect Number

Related Topics
C program to check whether a number is perfect number
C program to check whether a number is magic number
C program to check whether a number is prime or not
C program to check armstrong number
C program to check a number is palindrome or not
C program to convert decimal numbers to binary numbers
C program to make a simple calculator using switch statement
C program to convert temperature from celsius to fahrenheit
C program to print all prime factors of a number
List of all C programs