program execution wrong!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include <stdlib.h>

int perfect (int number);

int main(){

    int num;

    for(num=1;num<=1000;num++){    //  num<=2000000 takes 5509.372 second
        perfect(num);
    }



    return 0;
}
int perfect (int number){

    int counter,sum=0;

    for(counter=1;counter<number;counter++){

        if(number%counter==0){
            sum+=counter;
        }
    }
    if(sum==number){
        printf("%d is a perfect number!\n",number);
    }
}

6 is a perfect number!
28 is a perfect number!
496 is a perfect number!

Process returned 0 (0x0)   execution time : 0.037 s
Press any key to continue.


when I write code like this, the program work correctly

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include <stdlib.h>

int perfect (int number);

int main(){

    int num;

    for(num=1;num<=1000;num++){    //  num<=2000000 takes 5509.372 second
        printf("%d is a perfect number!\n",perfect(num));
    }



    return 0;
}
int perfect (int number){

    int counter,sum=0;

    for(counter=1;counter<number;counter++){

        if(number%counter==0){
            sum+=counter;
        }
    }
    if(sum==number){
        return sum;
    }
}



but when I write code like this program executes uncorrectly

WHY? and HOW can I fix it
Last edited on
In the second program you always print "... is a perfect number!"

Note that if you have a function with a return type other than void you should always make sure to return a value. In your first program the function never returns anything so the return type should probably be void. In the second program you only sometimes return a value, and the value that you return is the same as the argument. Is that really useful? Perhaps consider returning a boolean value, true (1) if the number is a perfect number and false (0) if the number is not a perfect number. Then you can check the return type to decide whether you should print "... is a perfect number!".
Last edited on
Code reposted with code tags.
https://www.cplusplus.com/articles/jEywvCM9/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#include <stdlib.h>

int perfect(int number);

int main()
{
  int num;
  for (num = 1; num <= 1000; num++) { // num<=2000000 takes 5509.372 second
    perfect(num);
  }
  return 0;
}

int perfect(int number)
{
  int counter, sum = 0;
  for (counter = 1; counter < number; counter++) {
    if (number % counter == 0) {
      sum += counter;
    }
  }
  if (sum == number) {
    printf("%d is a perfect number!\n", number);
  }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#include <stdlib.h>

int perfect(int number);

int main()
{
  int num;
  for (num = 1; num <= 1000; num++) { // num<=2000000 takes 5509.372 second
    printf("%d is a perfect number!\n", perfect(num));
  }
  return 0;
}

int perfect(int number)
{
  int counter, sum = 0;
  for (counter = 1; counter < number; counter++) {
    if (number % counter == 0) {
      sum += counter;
    }
  }
  if (sum == number) {
    return number;
  }
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
int main()
{
   const int n = 2000000;    // Gives 6, 28, 496, 8128
// const int n = 40000000;   // Needed for next one
   static unsigned long long sumProperFactors[1+n]{};
   for ( int i = 1; 2 * i <= n; i++ )
   {
      for ( int j = 2 * i; j <= n; j += i ) sumProperFactors[j] += i;
   }
   
   for ( int i = 1; i <= n; i++ ) if ( sumProperFactors[i] == i ) std::cout << i << " ";
}
Last edited on
I wanna do it only using function,repetition and selection because I am studying C from a book and I am now at function topic
One of the key aspects of learning programming is to learn how to adapt examples to suit a particular need. Trial and error is a great teacher.

lastchance's code is modifiable to be a function for the grunt work.
I changed code with

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <stdlib.h>

int perfect (int number);

int main(){

    int num;

    for(num=1;num<=1000;num++){    //  num<=2000000 takes 5509.372 second

        if(perfect(num)!=0){
             printf("%d is a perfect number!\n",perfect(num));
        }
    }



    return 0;
}
int perfect (int number){

    int counter,sum=0;
    int x=number;
    for(counter=1;counter<x;counter++){

        if(x%counter==0){
            sum+=counter;
        }
    }
    if(sum==number){
        return sum;
    }
    else{
        return 0;
    }
}


and it worked as I want. THANKS FOR HELPÄ°NG GUYS
Topic archived. No new replies allowed.