/* * Program demonstrating use of different kinds of loops to "count" * (print numbers from start to limit). * * Input: two integers n, m * * Output: all integers starting with n and ending with m. */ #include int main(void) { int start; int limit; int n; printf("enter start, limit\n"); if (scanf("%d %d", &start, &limit) != 2) { printf("error: input not numeric\n"); } else { if (start > limit) { printf("start %d is greater than limit %d\n", start, limit); } /* while loop */ printf("\ncounting with while loop:\n"); n = start; while (n <= limit) { printf("%d\n", n); n = n + 1; } /* for loop */ printf("\ncounting with for loop:\n"); for (n = start; n <= limit; n = n + 1) { printf("%d\n", n); } /* do-while loop (doesn't do what we intend) */ printf("\ncounting with do-while loop:\n"); n = start; do { printf("%d\n", n); n = n + 1; } while (n <= limit); } return 0; }