Home » C Programs » C Program For Perfect Number

C Program For Perfect Number

A positive integer that is a perfect number and equals the sum of its appropriate divisors. The sum of the lowest perfect number, 6, is made up of the digits 1, 2, and 3.

Why for loop is used in perfect number program in c?

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. The array and linked list are two common data structures that employ it. As the definition in this program for loop is used to execute the set statements for repeatedly.

If Else Statement in C

If you want to tell a block of code to run only if a certain condition is true, use the if statement. If the same condition is false, use else to declare that a different block of code should be run. If the first condition is false, use else if to define a new condition to test.

C Program For Perfect Number

#include<stdio.h>
int main()
 {
     int num, sum=0;
     printf("Enter Number");
     scanf("%d",&num);
     for(int i=1; i<=num/2; i++)
     {
         if(num%i==0)
             sum+=i;
     }
     if( sum==num )
         printf("%d is a Perfect Number\n",num);
     else
         printf("%d is Not a Perfect Number\n",num);
 }

Output:

Enter Number 5
5 is Not a Perfect Number