Answer L1

#include <stdio.h>
#include <stdlib.h>

/* Puzzle L01 -- print the integers from 0 to 14, one per line */
int main()
{
  int j;
  for (j=0; j<15; j++ )
  {
    printf("%3d\n", j);
  }
  system("PAUSE");	
  return 0;
}

Comment: This is the usual way to count from 0 to 14. It also works to use this for statement:

  for (j=0; j<=14; j++ )

Comment: The system("PAUSE") is needed if you run the program in some development environments. Without it, the program writes to a window which vanishes when the program reaches the end.

Comment: With most C compilers it works to declare the counter inside the for:

  for (int j=0; j<15; j++ )
  {
    printf("%3d\n", j);
  }

But this is not syntactically correct ANSI C. It is correct C++ which is why it works on most compilers. Some compilers will compile it, but complain.



Back to Puzzle Home