0

I've written a simple program to find the perfect number that is immediately after 28 (which is 496), however it is not working. Not sure what I'm doing wrong.

#include <stdio.h>

int main(){

    int num=29, sum=0, aux=1;

    while(aux!=0){
        for(int i=1; i<num; i++){
            if(!(num%i)){
                sum+=i;
            }
        }

        if(sum == num){
            printf("%d", sum);
            aux=0;
        }else{
            num++;
        }
    }

    return 0;
}
Krishna Kanth Yenumula
  • 2,533
  • 2
  • 14
  • 26
wintersun
  • 45
  • 6

2 Answers2

1

You must initialize sum before each check.

    while(aux!=0){
        sum = 0; /* add this */
        for(int i=1; i<num; i++){
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
0

You should have initialized the value of sum inside the while loop because it's value is not getting reset to 0 after a number check and it keeps on increasing the later value , so finally it would be resulting in a error .

yugsharma1711
  • 71
  • 1
  • 9