0
#include<stdio.h>
#include<math.h>

int main()
{
 int rem, num, i, sum;

 sum=0;
 num=28;

 for(i=1;i<num;i++)
 {
     if(num%i==0)
     {
         rem=num%i;
         sum=sum+rem;
     }
 }
 
 if(sum==num)
 {  
    printf("perfect number");
 }
 else
    printf("not perfect");
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • Does this answer your question? [Perfect Number In C](https://stackoverflow.com/questions/4554351/perfect-number-in-c) – John Cvelth Jan 23 '21 at 08:08

1 Answers1

0

why are you doing this, you need to get the total sum of all the proper divisors (proper divisor of a natural number is the divisor that is strictly less than the number) of num, but what you are doing is adding rem = num%i, which is basically always 0 as num is divisible by i what you checked in your if, so what you are doing is not making any sense

if(num%i==0)
{
    rem=num%i;  // here `rem` is always `0` as `num%i == 0`
    sum=sum+rem;  
}

rather your logic should be like below, as the divisor is i so you should add all the divisors (representing by the i when num is divisible by i) in your sum

if(num%i==0)
{
    sum = sum + i;
}
Sahadat Hossain
  • 3,583
  • 2
  • 12
  • 19
  • @saahilsabu you need to get the total sum of all the proper divisors of `num`, so now what you are doing is adding `rem = num%i`, which is basically always `0` as num is divisible by `i` what you checked in your `if`, so what you are doing is not making any sense – Sahadat Hossain Jan 23 '21 at 07:07
  • @saahilsabu i have also updated my answer explaining where you are making the mistake, have a look – Sahadat Hossain Jan 23 '21 at 07:11