revised: 01/06/2008
These puzzles involve simple loops.
[E-3]
Write a main()
program that
prints the integers 0 to 14 one per line. Write the integers to standard output.
On a Windows machine, write the integers to the DOS window. The
output of the program is:
0 1 2 3 . . . 14
Here is a skeleton of the program. Finish
it by completing the for
statement. With a programming environment
(such as Bloodshed Dev-C++) use copy-and-paste to copy this code
into the editor window and then save it to a source file,
or click here: Skeleton
#include <stdio.h> #include <stdlib.h> /* Puzzle A01 -- print the integers from 0 to 14, one per line */ int main(int argc, char *argv[]) { int j; for ( ) { printf("%3d\n", j); } system("PAUSE"); return 0; }
Even if the answer is obvious to you, finish the puzzle anyway. Compile it and run it to confirm that it works as you think it should. When you are done, click on the icon to see a suggested answer.
[E-3]
Write a main()
program that prints
the integers 1 to 15 one per line. The output of the program is:
1 2 3 . . . 15
Of course, a sensible way to do this is to edit the previous program. Or, click here for a skeleton: Skeleton
[E-3]
Write a main()
program that prints
the integers 0, 2, 4, up to 20 one per line. The output of the program is:
0 2 4 . . . 20
For an extra challenge, try to get this program correct on the first try. Based on what students do on tests and quizzes, I'd say you have a 10% chance of getting it wrong. Click here for a skeleton: Skeleton
[E-3]
Write a main()
program that prints
the integers -7 to +7, one per line. The output of the program is:
-7 -6 . . . -1 0 1 . . . 6 7
Edit the previous program to create this one, or click here for a Skeleton.
[E-3]
Write a main()
program that prints
the integers 15 down to 0 one per line. The output of the program is:
15 14 13 . . . 2 1 0
Edit the previous program to create this one, or click here for a Skeleton
[E-3]
Write a main()
program that prints
the odd integers 1 to 99, one per line. The output of the program is:
1 3 . . . 97 99
There are several ways to do this. Think a while about the choices, and then pick the best one. Click here for a Skeleton
[E-5]
Write a main()
program that first
asks the user for an integer, N, and the prints the first N odd integers starting
with 1, one per line. The output of the program is:
Enter N: 7 1 3 5 7 9 11 13
Here is a skeleton of the program (or click here: Skeleton):
#include <stdio.h> #include <stdlib.h> /* Puzzle A07 -- print the first N odd integers. Ask the user for N. */ int main(int argc, char *argv[]) { int j, N; printf("Enter N: "); scanf("%d", &N ) for ( ) { printf("%3d\n", ??? ); } system("PAUSE"); return 0; }
Try to write a nice, conceptually clean program. It is easy to make this program messier than it needs to be.
Thinking about odd and even is surprisingly common in programming, so this program is good practice.
[E-12]
Write a main()
program that
first asks the user for an integer, N, and then calculates three sums: the sum
of the even integers, the sum of the odd integers, and the sum of all integers
from 0 to N. The output of the program is:
Enter n: 7 Sum = 28, Sum of Odd = 16, Sum of Even = 12
Use the previous program as a start for this one (or click here: Skeleton). There are several ways to write this program. Try to write a nice, conceptually clean program.
The best way to write this program is to use the formulas that calculate each sum in one step. But, to practice programming, implement this program with a loop.
[E-13]
Write a main()
program that
first asks the user for an integer, N, and then calculates three sums: the sum
of the even integers, the sum of the odd integers, and the sum of all integers
from 0 to N. However, exclued from these sums those integers that are
divisible by 3 or divisible by 4. The output of the program is:
Enter n: 10 Sum = 25, Sum of Odd = 13, Sum of Even = 12
Use the previous program as the basis for this one (or click here: Skeleton).
[E-5]
Write a main()
program that prints
the integers from 0 to 24 (inclusive). Print five integers per line. The output
of the program is:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Often in programming you want to format the output in a style similar to this. If you have not seen this before, it may take some thought to figure out how to do it. (Hint: Look at the last integer of each line and use the modulo operator %).
Use puzzle A03 as the basis for this one (or click here: Skeleton).