|
CS256 - Fundamentals of Structured Programming
| Displaying exercises/e6/solution/p1.c
#include <stdio.h>
/**
* Print "Hello, world!" preceded by a number that is right aligned to three
* spaces and ranges from 1 to 100 in steps of 9. You must use a loop for this
* problem. The number should be right aligned to 3 columns. To right align
* numbers, use %#d in your printf, where # is the number of columns you want.
* Example output:
* 1 Hello, world!
* 10 Hello, world!
* 19 Hello, world!
* 28 Hello, world!
* ...
* 100 Hello, world!
*/
int main(void)
{
int line;
int lower, upper, step;
// Values in variables to make the code more readble:
lower = 1;
upper = 100;
step = 9;
// Start counting at the lower end value:
line = lower;
// While the line hasn't reached the upper value, loop:
while (line <= upper) {
printf("%3d Hello, world!\n", line);
// Increase the line number by the step value:
line = line + step;
}
return 0;
}
|