Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    C Program to Check Whether a Number Is Perfect or Not

    C Programming Examples


    Learn how to write a C program to effectively check whether a number is perfect or not with our simple and easy-to-follow tutorial.

    What is a Perfect Number?

    A perfect number is a positive whole number where all the smaller numbers that can divide into it, when added together, give the original number. For example, 6 is a perfect number because when you add up its smaller numbers that can divide into it (1, 2, and 3), the total is 6. The first five known perfect numbers are 6, 28, 496, 8128, and 33550336.

    C Program to Check If a Number is Perfect

    Here's a C program that determines if a given number is perfect:

    #include <stdio.h>
    
    // Function to check if a number is perfect
    int isPerfect(int num) {
        int sum = 0, i;
        
        // Loop through numbers less than 'num'
        for(i = 1; i < num; i++) {
            // Check if 'i' is a divisor of 'num'
            if(num % i == 0) {
                sum += i; // Add the divisor to 'sum'
            }
        }
    
        // Return true if 'sum' is equal to 'num'
        return (sum == num);
    }
    
    // Main function
    int main() {
        int num;
    
        // Ask the user for the number
        printf("Enter a number: ");
        scanf("%d", &num);
    
        // Check if the number is perfect
        if(isPerfect(num)) {
            printf("%d is a perfect number.\n", num); // Output if number is perfect
        } else {
            printf("%d is not a perfect number.\n", num); // Output if number is not perfect
        }
        
        return 0;
    }

    Program Output:

    Enter a number: 8
    8 is not a perfect number.
    
    Enter a number: 28
    28 is a perfect number.

    Code Explanation:

    The above program is split into two main parts: the isPerfect function and the main function.

    • The isPerfect Function: This function accepts an integer and returns whether it's a perfect number or not. A for loop iterates through numbers less than the given integer, checking if they're divisors. If they are, the function adds them to the sum. Finally, it checks if the sum equals the original number, returning the result.
    • The main function asks for a number from the user. It then calls the isPerfect function and, based on the return value, prints whether the number is perfect or not.

    Conclusion

    In this tutorial, you learned how to write a C program that checks whether a number is perfect or not. This simple program uses basic C programming constructs like loops and functions. By understanding and implementing this code, you have enhanced your grasp of these essential programming elements.



    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.