while( x < 10 ); { printf( "%d", x ); }
x = 0; while( x < 10 ) { printf( "%d\n", x ); x++; }
scanf( "%d", &x ); while( x != 9999 ) { printf( "%d\n", x ); scanf( "%d", &x ); }
for( int x=20; x>=1;x-- ) { for( int y=x; y>=1;y--) printf( "%3d",x); printf("\n"); }
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
========== * * * * * * * * * * ==========
* ** *** ****
/* EX2-4.C from Teach Yourself C in 21 Days */ #include <stdio.h> main() { int ctr; for( ctr = 65; ctr < 91; ctr++ ) printf("%c", ctr ); return 0; } /* end of program */
A. 10 B. 9 C. 0 D. 1
A. When x is less than one hundred B. When x is greater than one hundred C. When x is equal to one hundred D. While it wishes
A. for B. do while C. while D. repeat until
A. 0 B. Infinitely C. 1 D. Variable
for(i = 0; i < 10; i = i + 1) printf("i is %d\n", i);
int i; for(i = 0; i < 3; i = i + 1) printf("a\n"); printf("b\n"); printf("c\n");
#include <stdio.h> int main() { int i; printf("statement 1\n"); printf("statement 2\n"); for(i = 0; i < 10; i = i + 1) { printf("statement 3\n"); printf("statement 4\n"); } printf("statement 5\n"); return 0; }This program doesn't do anything useful; it's just supposed to show you how control flow works--how statements are executed one after the other, except when a construction such as the for loop alters the flow by arranging that certain statements get executed over and over. In this program, each simple statement is just a call to the printf function.
Now delete the braces {} around statements 3 and 4, and re-run the program. How does the output change? (See also question 8 above.)
#include <stdio.h> int main() { int i, j; printf("start of program\n"); for(i = 0; i < 3; i = i + 1) { printf("i is %d\n", i); for(j = 0; j < 5; j = j + 1) printf("i is %d, j is %d\n", i, j); printf("end of i = %d loop\n", i); } printf("end of program\n"); return 0; }
#include <stdio.h> int main() { int day, i; for(day = 1; day <= 3; day = day + 1) { printf("On the %d day of Christmas, ", day); printf("my true love gave to me\n"); for(i = day; i > 0; i = i - 1) { if(i == 1) { if(day == 1) printf("A "); else printf("And a "); printf("partridge in a pear tree.\n"); } else if(i == 2) printf("Two turtledoves,\n"); else if(i == 3) printf("Three French hens,\n"); } printf("\n"); } return 0; }The result (as you might guess) should be an approximation of the first three verses of a popular (if occasional tiresome) song.
a. Add a few more verses (calling birds, golden rings, geese a-laying, etc.).
b. Here is a scrap of code to print words for the days instead of digits:
if(day == 1) printf("first"); else if(day == 2) printf("second"); else if(day == 3) printf("third");Incorporate this code into the program.
c. Here is another way of writing the inner part of the program:
printf("On the %d day of Christmas, ", day); printf("my true love gave to me\n"); if(day >= 3) printf("Three French hens,\n"); if(day >= 2) printf("Two turtledoves,\n"); if(day >= 1) { if(day == 1) printf("A "); else printf("And a "); printf("partridge in a pear tree.\n"); }Study this alternate method and figure out how it works. Notice that there are no else's between the if's.
If you keep track of the sum in a variable of type int, and divide by the integer 10, you'll get an integer-only approximation of the average (the answer should be 38). If you keep track of the sum in a variable of type float or double, on the other hand, you'll get the answer as a floating-point number, which you should print out using %f in the printf format string, not %d. (In a printf format string, %d prints only integers, and %f is one way to print floating-point numbers. In this case, the answer should be 38.5.)
1 is odd 2 is even 3 is odd ...(Hint: use the % operator.)