Solution for Exercise 11-3
ex1103
#include <stdio.h> int main() { int c; for(c=-5;c<5;c++) printf("%d ",c); for(;c>=-5;c--) printf("%d ",c); putchar('\n'); return(0); }
Output
-5 -4 -3 -2 -1 0 1 2 3 4 5 4 3 2 1 0 -1 -2 -3 -4 -5
Notes
* Loops in C can spin in only one direction. While you could craft a tricky loop that changed direction, it's just easier to use two loops, one for each direction.
* The decrement operator appears at Line 9 in the for statement.
* The blank space after %d
in both Lines 8 and 10 keeps the output looking clean.
* The putchar() statement ensures that the output appears on a line by itself.
Copyright © 1997-2024 by QPBC.
All rights reserved