For Loops

For loops provide the ability to execute a block of code multiple times while updating a counter to a destination target. An example would be the construct

for (i = 0; i < 5; i++) printf("*");

Which will print out five asterisks in a row as the loop will set i from 0 to 5 but execute the target of the loop from 0 to 4. After the loop finishes, a will have a value of 5.

Write a program that prints the numbers from 1 through 10 on the same line, then prints the numbers from 10 through 1 on the next line, then the numbers from 0 through 20 by two (0 2 4 ... 18 20), and finally prints one *, then two, then three up until 10.

Expected Output

F:\Mozilla>a
1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1
0 2 4 6 8 10 12 14 16 18 20
*
**
***
****
*****
******
*******
********
*********
**********

Solution


Return to Homepage    Return to Programming Pages
Updated November 24, 2005. For comments and questions, send email to Vector.x64 @ gmail.com (without the spaces).