lmc
lmc

Reputation: 175

Nested for loops in C language

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?

Upvotes: 1

Views: 439

Answers (4)

#include<stdio.h> 

int main(void) {

int upper_limit,candidate,divisor,s;

scanf("%d", &upper_limit); 

for(candidate=1; candidate<=upper_limit; candidate++) {

    s=0; 

    for(divisor=1; divisor<=(candidate/2); divisor++) {

        if((candidate%divisor)==0) 

            s=s+divisor:
    }

if(candidate<s) 
    printf("%d"\n", candidate);

}

return 0;

} 

Upvotes: 0

KingOfWigs
KingOfWigs

Reputation: 17

I have written a simpler implementation. This is same as your own attempt. just that it is enough to run till k/2 instead of k-1

    #include<stdio.h>
    int main(void) {

        int k,i,j,s;
        scanf("%d", &k);
        s=0;
        for(i=1; i<=(k/2); i++) { //running till half of the entered value is sufficient
            if(k%i == 0)
            {
                s+=i;  //if it is a divisor add it to the sum
            }
        }
        printf("sum is %d\n\r",s);
        if(k<s)
           printf("%d is abundant\n\r",k);
        else
           printf("%d is not abundant\n\r",k);
        return 0;
    }

Upvotes: 0

4386427
4386427

Reputation: 44274

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);
}

Upvotes: 1

the inner loop is executed k time;

  1. when i=1 nothing happens
  2. when i=2 the inner loop checks if 2 is abundant or not
  3. when i=3 the inner loop checks if 3 is abundant or not

and so on. Maybe it would be better to rewrite your code so that variable names explains its meaning.

Upvotes: 0

Related Questions