C program to find if the given number is perfect number or not

CServer Side ProgrammingProgramming

Perfect number is the number; whose sum of factors is equal to 2*number.

Algorithm

An algorithm is explained below −

START
Step 1: declare int variables and initialized result=0.
Step 2: read number at runtime.
Step 3: for loop i=1;i<=number;i++
Condition satisfies
   i. if(number%i==0)
   ii. result=result+i;
Step 4: checking the sum of factors.
   i. if(result==2*number)
   ii. print perfect number
   iii. else print not perfect number
STOP

Program

Following is the C program to find if the given number is perfect number or not −

 Live Demo

#include<stdio.h>
int main(){
   int number,i,result=0;//declare variables and initialize result to 0
   printf("enter the number:");
   scanf("%d",&number);
   for(i=1;i<=number;i++){
      if(number%i==0)
         result=result+i;
   }
   if(result==2*number) //checking the sum of factors==2*number
      printf("perfect number");
   else
      printf("not perfect number");
}

Output

The output is given below −

enter the number:28
perfect number
enter the number:46
not perfect number
raja
Published on 10-Mar-2021 09:57:38
Advertisements