Perfect number in C

Check Number is Perfect or Not

C program to check number is a perfect number or not

A 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
Divisors of 6 are 1, 2, 3
Sum of its divisors = 1 + 2 + 3 = 6
Hence 6 is a perfect number.

So, let’s move to our program:

#include <stdio.h>
#include<conio.h>
 
void main()
{
    int number, rem, sum = 0, i;
 clrscr();
 printf("\t\t\t...Welcome To EduNews.Tech...");
    printf("\n\nEnter a Number : ");
    scanf("%d", &number);
    for (i = 1; i <= (number - 1); i++)
    {
    rem = number % i;
    if (rem == 0)
    {
        sum = sum + i;
    }
    }
    if (sum == number)
    printf("Entered Number is perfect number");
    else
    printf("Entered Number is not a perfect number");
printf("\n\n\n\t\t\tThankyou for Joining Us !");
printf("\n\t\t\t!Regards EduNews !");
getch();
} 

Program Output:

perfect number output

Explanation

Today, we are learning the given number is perfect or not value using the ‘number’ variable. A perfect number is a number that is equal to the sum of its divisor. For example, divisors of 6 are 1, 2, and 3. The sum of these divisors is 6. So the number 6 is called a perfect number.

For loop, the statement is used to assign the modulus of the value of the ‘number’ variable by the value of the ‘i’ variable. if condition statement is used to check the value of the ‘rem’ variable is equal to 0, if the condition is true to execute if condition statement and compute the summation the value of ‘sum’ variable with the value of ‘i’ variable.

Another If-else condition statement is used to check that both the value of the ‘sum’ variable and the value of the ‘number’ variable are equal, if the condition is true print the statement as Entered Number is perfect number. Otherwise, execute else condition statement and print the statement as Entered Number is not a perfect number.

I hope this post helps you to understand Perfect number and its implementation in C programming language.

Keep coding 🙂

4 thoughts on “Perfect number in C”

Leave a Reply

Your email address will not be published. Required fields are marked *