Puzzle L12


Print the ODD integers from start down to finish, seven per line

[E-8]Write a main() program that prints the odd integers from start down to finish, seven per line. Make start and finish variables that are initialized by assignment (or by user input, if you want.) When start==147, and finish==59, the output of the program is:

 147 145 143 141 139 137 135
 133 131 129 127 125 123 121
 119 117 115 113 111 109 107
 105 103 101  99  97  95  93
  91  89  87  85  83  81  79
  77  75  73  71  69  67  65
  63  61  59

The integers that end each line are less regular than in previous programs. It is probably wise to maintain a separate variable that counts the integers as they are printed. The last line does not contain the full five integers, but must end with "\n"

Here is a skeleton:

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

/* Puzzle L12 -- print the ODD integers from start down to finish
|  seven per line.
|  End the last line with "\n", even if the line has fewer than
|  seven numbers on it.
|
*/
int main()
{
  int j;
  int start=147, finish=53;
  int count = 0;
  
  for (j=start; j>=finish; j -= 2 )
  {

  }

  /* Last Line Logic */
  if ( count%7 != 0 ) printf("\n");
  
  
  return 0;
}

Your odds of getting this program correct on the very first try are about one in five. See if you can beat the odds!



Answer         Next Page         Previous Page Home