3

I am trying to generate numbers from 1-5 using for loop and display their sum. I am aiming to have an output that looks like this:

1 + 2 + 3 + 4 + 5 = 15

But instead, I get the output:

+ 1 + 2 + 3 + 4 + 5 = 15

    #include <stdio.h>

    void main()
    {   
        int a, sum = 0;
        for (a = 1; a <= 5; a++) 
        { 
            printf("+\t%d\t",a);
            sum = sum + a;
        }
        printf("=\t%d", sum);       
    }
paddy
  • 60,864
  • 6
  • 61
  • 103
Sue
  • 33
  • 2

2 Answers2

2

This is encountered very frequently whenever outputting lists separated by some value. The problem is that you have 5 values to output, but only 4 separators. If you don't do anything special, then you will output 5 separators which is exactly what's happening in your example. Even if you move the separator to after your value, you'll still have one too many.

The way I prefer to do it is this:

for (a = 1; a <= 5; ++a)
{
    if (a > 1) printf("\t+\t");
    printf("%d", a);
    sum += a;
}

The reason I prefer this approach, versus outputting some value outside of the loop, is because often the thing you're outputting is more complicated, possibly involving additional calculation or function calls and I don't like duplicating that code.

So, I only output a separator if I know that I'm about to output the other thing. That means, output a separator for every loop iteration except the first.

I also like doing it prefix-style because usually the condition for the first item in a loop is simpler than the condition for the last item. It's also compatible with a different approach involving a flag:

int first = 1;
for (a = 1; a <= 5; ++a)
{
    if (!first) printf("\t+\t");
    first = 0;
    printf("%d", a);
    sum += a;
}

There are many other ways you'll see this kind of pattern occur. And there may be various forms of optimizing it that reduce readability. But this approach is simple and easy to follow.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
paddy
  • 60,864
  • 6
  • 61
  • 103
1

You can take pointer of type char and assign it an empty string as a separator for the first iteration and after printing first number assign the separator string, to the pointer, that you want to print between two number in further iterations.

Implementation:

#include <stdio.h>

int main (void) {   
    int a, sum = 0;
    char * str = "";

    for (a = 1; a <= 5; a++) { 
        printf("%s%d\t", str, a);
        sum = sum + a;
        str = "+\t";
    }

    printf("=\t%d\n", sum);       
    return 0;
}

Output:

# ./a.out
1   +   2   +   3   +   4   +   5   =   15

Additional:

  • Using void as return type of main() function is not as per standards. The return type of main() function should be int.
H.S.
  • 11,654
  • 2
  • 15
  • 32
  • I like this; even more compact, `printf("%s%d\t", a == 1 ? "" : "+\t", a)`. – Neil Dec 20 '21 at 06:30
  • 1
    @Neil More compact solution, just one statement in loop - `printf("%s%d\t", a == 1 ? "" : "+\t", (sum = sum + a, a));`.... :). – H.S. Dec 20 '21 at 07:32