Project Euler problem #1 solution in C Any suggestions or comments? Is my style any good? 

Initially I struggled with this problem as I found that using the "+=" operator gave the wrong results. I also tried a while loop the first time which was a baaaad idea. But what actually worked was using a static accumulator variable.

#include &lt;stdio.h&gt;





static unsigned long total;

int main(void)
{


  total = 0;





  for(unsigned int i = 1; i &lt; 334; i++){

     total  = total + (3*i);


  }




  for(unsigned int j = 1; j &lt; 1000; j++){

    if(j%5==0 &amp;&amp; j%3!=0){

      total  = total + j;

    }else{

      total = total + 0;

    }


  }



  printf("Project Euler problem 1 solution = ");
  printf("%lu \n", total);


  return 0;
}


Ref: Euler 1: Multiples of 3 and 5
Your code is very hard to read since there is so much empty space. Even if we don't remove all of them, the following variant is a lot easier to read:

#include &lt;stdio.h&gt;

static unsigned long total;

int main(void)
{
  total = 0;

  for(unsigned int i = 1; i &lt; 334; i++){
     total  = total + (3*i);
  }

  for(unsigned int j = 1; j &lt; 1000; j++){

    if(j%5==0 &amp;&amp; j%3!=0){
      total  = total + j;
    }else{
      total = total + 0;
    }
  }

  printf("Project Euler problem 1 solution = ");
  printf("%lu \n", total);

  return 0;
}


It fits even into the code example without any scrollbars.



There is no need to make total global. It should be a local variable.



Your first for loop uses an optimization, your second doesn't. That's asymmetrical. Use the same trick for both loops.



total = total + 0 doesn't change total at all. The compiler will most likely throw that line away, but better don't include it.



The following for loop is easier to understand:

for(unsigned int i = 0; i &lt; 1000; i += 3) {
    total += i;
}




Concerning your comments on +=, did you accidentally use total += total + j? This will give wrong results. Otherwise I cannot reproduce your behaviour.



If you printf a unsigned int, you must use %u, not %lu, since the latter is meant for unsigned long int. Your compiler will warn you mismatching specifiers if you enable warnings.



If you apply those comments above, you end up with

#include &lt;stdio.h&gt;

int main(void)
{
  unsigned int total = 0;

  for(unsigned int i = 0; i &lt; 1000; i += 3){
     total += i;
  }

  for(unsigned int i = 0; i &lt; 1000; i += 5){    
    if(i % 3 != 0){
      total += i;
    }
  }

  printf("Project Euler problem 1 solution = %u \n", total);

  return 0;
}