Q:

C program to check whether number is Perfect Number or not

0

C program to check whether number is Perfect Number or not

Perfect Number - A perfect number is a positive integer number which is equals to the sum of it's proper positive divisors excluding the number itself. For example 6 is a perfect number because it’s proper divisors are 1, 2, 3 and it’s sum is equals to 6.

In this program we will read an integer number and check whether it is Perfect Number or not. To check perfect number we have to find all divisors of that number and find their sum, if sum of divisors is equal to number it means number is Perfect Number.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Check Perfect Number using C program

/*C program to check nunber is perfect or not.*/

#include <stdio.h>

int main()
{
    int num, loop;
    int sum;

    printf("Enter an integer number: ");
    scanf("%d", &num);

    sum = 0;

    for (loop = 1; loop < num; loop++) {
        if (num % loop == 0)
            sum += loop;
    }

    if (sum == num)
        printf("%d is a perfect number.", num);
    else
        printf("%d is not a perfect number.", num);

    return 0;
}

Using User Define Function

/*C program to check nunber is perfect or not.*/

#include <stdio.h>

/*function to check perfect number or not*/
int isPerfect(int num)
{
    int loop, sum = 0;

    for (loop = 1; loop < num; loop++) {
        if (num % loop == 0)
            sum += loop;
    }

    if (sum == num)
        return 1; /*Perfect Number*/
    else
        return 0; /*Not Perfect Number*/
}

int main()
{
    int num, loop;
    int sum;

    printf("Enter an integer number: ");
    scanf("%d", &num);

    if (isPerfect(num))
        printf("%d is a perfect number.", num);
    else
        printf("%d is not a perfect number.", num);

    return 0;
}

Output:

    First Run:
    Enter an integer number: 6
    6 is a perfect number.

    Second Run:
    Enter an integer number: 496
    496 is a perfect number.

    Third Run:
    Enter an integer number: 695
    695 is not a perfect number.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C program to check whether an integer number is po... >>
<< C program to count occurrence of a particular digi...