Q:

C Program to find Perfect Number

belongs to collection: C Programming on Numbers

0

In this exercise, we learn how to write a C Program to find Perfect Number?. We will write the C program to find Perfect Number using the Arithmetic Operators. Write a function to check if a given number is perfect or not. How to check the perfect number in C programming using loop. Logic to check perfect number in C programming.

All Answers

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

#include <stdio.h>
int main()
{
    int i, num, sum = 0;
    //Ask user to enter a number
    printf("Enter any number = ");
    scanf("%d", &num);
    // Calculate sum of all proper divisors
    for(i = 1; i <= num / 2; i++)
    {
        //if i is a proper divisor of num
        if(num%i == 0)
        {
            sum += i;
        }
    }
    // Check whether the sum of proper
    //divisors is equal to num or not
    if(sum == num)
    {
        printf("%d is perfect number", num);
    }
    else
    {
        printf("%d is not perfect number", num);
    }
    return 0;
}

Output:

Enter any number = 6
6 is a perfect number

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

total answers (1)

C Programming on Numbers

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to check positive or negative without us... >>
<< C Program to swap two numbers using arithmetic ope...