0





7

Advertisement

An abundant number is a natural number that is less than the sum of its proper divisors. For example 12 < 1+2+3+4+6=16 so 12 is an abundant number, while 16 > 1+2+4+8=15 is not an abundant number. I have to write a program in C language so that for the input k, the output are all abundant numbers less than or equal to k.

I'm only a beginner at this so what I first wanted to do is to write a program that will check whether k is abundant or not. So this is what I did:

#include <stdio.h>

int main(void) {
    int k, i, s = 0;
    scanf("%d", &k);
    for (i = 1; i < k; i++) {
        if (k % i == 0)
            s = s + i;
    }
    if (k < s)
        printf("%d" is an abundant number", k);
    return 0;
}

Feel free to ignore this above, I only wanted to show you I actually tried something by myself. Now I wasn't sure how to make this program list the abundant numbers that are also less than k, but I found the solution which I don't understand:

#include <stdio.h>

int main(void) {
    int k, i, j, s;
    scanf("%d", &k);
    for (i = 1; i <= k; i++) {
        s = 0;
        for (j = 1; j < i; j++) {
            if (i % j == 0)
                s = s + j:
        }
        if (i < s)
            printf("%d"\n", i);
    }
    return 0;
}

I'm confused by this nested for loop, can someone explain exactly how it works? For instance if we put k=18, what exactly happens with this for loops so that in the end we get 12 and 18 as output?

Question author Lewis | Source

Advertisement


0


I think the best way is to step through the code manually and write down the line number of the executing code and how the variables change.

Like

L01:    int k,i,j,s;                // k=?, i=?, j=?, s=?
L02:    scanf("%d", &k);            // k=18, i=?, j=?, s=?
L03:    for(i=1;                    // k=18, i=1, j=?, s=?
L03:             i<=k;              // TRUE
L04:        s=0;                    // k=18, i=1, j=?, s=0
L05:        for(j=1;                // k=18, i=1, j=1, s=0
L05:                 j<i;           // FALSE
L03:                      i++)      // k=18, i=2, j=1, s=0
L03:             i<=k;              // TRUE
L04:        s=0;                    // k=18, i=2, j=1, s=0
L05:        for(j=1;                // k=18, i=2, j=1, s=0
L05:                 j<i;           // TRUE
L06:            if(i%j==0)          // TRUE
L07:                s=s+j:          // k=18, i=2, j=1, s=1
L05:                      j++)      // k=18, i=2, j=2, s=0
L05:                  j<i;          // FALSE
and so on ....

I takes quite some time but you should soon see the pattern and there by understand how for-loops works.

Another thing that might help you understanding for-loops are to realize that

for(i=0; i<N; i++)
{
    code...
}

is equivalent to

i=0;
while (i<N)
{
    code...

     i++;
}

BTW:

Always check the return value from scanf - like:

if (scanf("%d", &k) != 1)
{
    printf("Input error! Program terminates.\n");
    exit(1);
}
Answer author 4386427