C/C++
Table of contents 7. 1. 2. while 3. 4. 5. for 6. 8. (do while) 9. 10. (nested loop) 11. 12. 13. 1
// summing.c: # include <stdio.h> int main ( void ) { long num ; long sum = 0L; int status ; printf (" Enter an integer to be summed "); printf ("(q to quit ): "); status = scanf ("%ld", & num ); while ( status == 1) { sum = sum + num ; printf (" Enter next integer (q to quit ): "); status = scanf ("%ld", & num ); } printf (" Those integers sum to %ld.\n", sum ); return 0; } 2
Enter an integer to be summed ( q to quit ): 22 Enter next integer ( q to quit ): 33 Enter next integer ( q to quit ): 44 Enter next integer ( q to quit ): q Those integers sum to 99. 3
while
while while ( condition ) statement while ( condition ) { statements } 4
while no go to next statement condition yes { } statements; 5
while while while 6
while while index = 1; while ( index < 5) { printf (" Good morning!\n"); } 7
while while index = 1; while ( index < 5) { printf (" Good morning!\n"); } index 7
while while index = 1; while (-- index < 5) { printf (" Good morning!\n"); } 8
while while index = 1; while (-- index < 5) { printf (" Good morning!\n"); } index 8
while while index = 1; while (++ index < 5) { printf (" Good morning!\n"); } 9
while while index = 1; while (++ index < 5) { printf (" Good morning!\n"); } 9
while 10
while 1 // when.c: 2 # include <stdio.h> 3 int main ( void ) 4 { 5 int n = 5; 6 while (n < 7) { 7 printf ("n = %d\n", n); 8 n ++; 9 printf (" Now n = %d\n", n); 10 } 11 return 0; 12 } n = 5 Now n = 6 n = 6 Now n = 7 11
while while while 12
while index = 10; while ( index ++ < 5) printf (" Have a fair day or better.\n"); 13
while index = 10; while ( index ++ < 5) printf (" Have a fair day or better.\n"); index = 3; 13
while while 14
while 1 // while1.c: 2 # include <stdio.h> 3 int main ( void ) 4 { 5 int n = 0; 6 while ( n < 3) 7 printf ("n = %d\n", n); 8 n ++; 9 printf (" That s all this " 10 " program does.\n"); 11 return 0; 12 } 15
while 1 // while1.c: 2 # include <stdio.h> 3 int main ( void ) 4 { 5 int n = 0; 6 while ( n < 3) 7 printf ("n = %d\n", n); 8 n ++; 9 printf (" That s all this " 10 " program does.\n"); 11 return 0; 12 } n = 0 n = 0 n = 0 n = 0... 15
while while while 16
while 1 // while2.c: 2 # include <stdio.h> 3 int main ( void ) 4 { 5 int n = 0; 6 while (n++ < 3); 7 printf ("n = %d\n", n); 8 printf (" That s all this program does.\n"); 9 return 0; 10 } 17
while 1 // while2.c: 2 # include <stdio.h> 3 int main ( void ) 4 { 5 int n = 0; 6 while (n++ < 3); 7 printf ("n = %d\n", n); 8 printf (" That s all this program does.\n"); 9 return 0; 10 } n = 4 That s all this program does. 17
while C (null statement) 18
while while 19
while while while ( scanf ("%d",& num ) ==1) ; 19
while while while ( scanf ("%d",& num ) ==1) ; while 19
1: < <= > >= ==!= 20
while C 21
while ( number < 6) { printf (" Your number is too small.\n"); scanf ("%d", & number ); } 22
while ( number < 6) { printf (" Your number is too small.\n"); scanf ("%d", & number ); } while (ch!= * ) { count ++; scanf ("%c", &ch); } 22
while ( number < 6) { printf (" Your number is too small.\n"); scanf ("%d", & number ); } while (ch!= * ) { count ++; scanf ("%c", &ch); } while ( scanf ("%f", & num ) == 1) sum = sum + num ; 22
23
< > 3 1 == 1.0 3 6 1/3 0.999 999 23
< > 3 1 == 1.0 3 6 1/3 0.999 999 math.h fabs() 23
// cmpflt.c: # include <stdio.h> # include <math.h> int main ( void ) { const double PI = 3. 14159; double response ; printf (" What is the value of pi?\n"); scanf ("%lf", & response ); while ( fabs ( response -PI) >0.0001) { printf (" Try again!\n"); scanf ("%lf", & response ); } printf (" Close enough!\n"); return 0; } 24
What is the value of pi? 3.14 Try again! 3. 1416 Close enough! 25
// t_and_f.c # include <stdio.h> int main ( void ) { int true_ val, false_ val ; true_ val = (10 > 2); false_ val = (10 == 2); printf (" true = %d; false = %d\n", true_val, false_val ); return 0; } 26
// t_and_f.c # include <stdio.h> int main ( void ) { int true_ val, false_ val ; true_ val = (10 > 2); false_ val = (10 == 2); printf (" true = %d; false = %d\n", true_val, false_val ); return 0; } true = 1; false = 0 26
C 1 0 27
C 1 0 while (1) {... } 27
1 0 while 28
i // truth.c # include <stdio.h> int main ( void ) { int n = 3; while (n) printf ("%2d is true \n", n - -); printf ("%2d is false \n", n); n = -3; while (n) printf ("%2d is true \n", n ++) ; printf ("%2d is false \n", n); return 0; } 29
3 is true 2 is true 1 is true 0 is false -3 is true -2 is true -1 is true 0 is false 30
C/C++ 0 31
C/C++ 0 while (n!= 0) {... } while (n) {... } 31
// trouble.c: # include <stdio.h> int main ( void ) { long num ; long sum = 0L; int status ; printf (" Enter an integer to be summed "); printf ("(q to quit ): \n"); status = scanf ("%ld", & num ); while ( status = 1) { sum = sum + num ; printf (" Enter next integer (q to quit ):\n"); status = scanf ("%ld", & num ); } printf (" Those integers sum to %ld.\n", sum ); return 0; } 32
Enter an integer to be summed ( q to quit ): 1 Enter next integer ( q to quit ): 2 Enter next integer ( q to quit ): 3 Enter next integer ( q to quit ): q Enter next integer ( q to quit ): Enter next integer ( q to quit ): Enter next integer ( q to quit ):... 33
while status=1 status==1 status=1 1 while ( status = 1) while (1) 34
q scanf() scanf() q q q scanf() 35
== = = == i = 5 // i 5 i == 5 // i 5 == 5 = i // 5 == i // i 5 36
1 0 while if 37
_Bool / C int C99 _Bool 1 0 1 C 38
_Bool // boolean.c: # include <stdio.h> int main ( void ) { long num ; long sum = 0L; _Bool input_ is_ good ; printf (" Enter an integer to be summed "); printf (" (q to quit ):\n"); input_is_good = ( scanf ("%ld", & num ) == 1); while ( input_ is_ good ) { sum = sum + num ; printf (" Enter next integer (q to quit ):\n"); input_is_good = ( scanf ("%ld", & num ) == 1); } printf (" Those integers sum to %ld.\n", sum ); return 0; } 39
_Bool input_is_good = ( scanf ("%ld", & num ) == 1); == == = while ( input_is_good ) 40
_Bool C99 stdbool.h bool _Bool true false 1 0 41
_Bool // boolean1.c: # include <stdio.h> # include < stdbool.h> int main ( void ) { long num ; long sum = 0L; bool input_ is_ good ; printf (" Enter an integer to be summed "); printf (" (q to quit ):\n"); input_is_good = ( scanf ("%ld", & num ) == true ); while ( input_ is_ good ) { sum = sum + num ; printf (" Enter next integer (q to quit ):\n"); input_is_good = ( scanf ("%ld", & num ) == true ); } printf (" Those integers sum to %ld.\n", sum ); return 0; } 42
+ - x > y+2 x > (y +2) x = y > 2 x = ( y > 2) x_ bigger = x > y x_ bigger = ( x > y) 43
2: < <= > >= ==!= 44
c!= a == b ( c!= a) == b 45
() - + ++ -- sizeof (type) * / % + - < <= > >= ==!= = 46
47
// sweetie1.c: # include <stdio.h> int main ( void ) { const int NUMBER = 2; int count = 1; while ( count <= NUMBER ) { printf (" Hello world!\n"); count ++; } return 0; } 48
49
while ( count <= NUMBER ){... count ++; } = while ( count ++ <= NUMBER ){... } 50
for
for for ( initialization ; condition ; increment ) { statements } for ( initialization ; condition ; increment ) statement for 51
for // sweetie2.c: # include <stdio.h> int main ( void ) { const int NUMBER = 4; int count ; for ( count = 0; count < NUMBER ; count ++) printf (" Hello world!\n"); } return 0; 52
for Hello world! Hello world! Hello world! Hello world! 53
for for 1. for 2. 3. 54
for # include <stdio.h> int main ( void ) { int num ; printf ("%3s %8s\n", "n", "n cubed "); for ( num = 1; num <= 4; num ++) printf ("%3d %8d\n", num, num * num * num ); return 0; } 55
for n n cubed 1 1 2 8 3 27 4 64 56
for for 1 // for_down.c: # include <stdio.h> int main ( void ) { int secs ; for ( secs = 4; secs > 0; secs - -) printf ("%d seconds!\n", secs ); printf (" Ignition!\n"); return 0; } 57
for for 4 seconds! 3 seconds! 2 seconds! 1 seconds! Ignition! 58
for for 2 2 10 // for_13s.c: # include <stdio.h> int main ( void ) { int n; for ( n = 2; n < 60; n = n +13) printf ("%d\n", n); return 0; } 59
for for 2 15 28 41 54 60
for for 3 // for_char.c: # include <stdio.h> int main ( void ) { char ch; for (ch = a ; ch <= z ; ch ++) printf (" The ASCII value of %c is %d\n", ch, ch); return 0; } 61
for for The ASCII value of a is 97 The ASCII value of b is 98... The ASCII value of y is 121 The ASCII value of z is 122 62
for for 4 // for_cube1.c: # include <stdio.h> int main ( void ) { int num ; printf ("%3s %8s\n", "n", "n cubed "); for ( num = 1; num * num * num <= 64; num ++) printf ("%3d %8d\n", num, num * num * num ); return 0; } 63
for for n n cubed 1 1 2 8 3 27 4 64 64
for for 5 // for_geo.c: # include <stdio.h> int main ( void ) { double debt ; for ( debt = 100. 0; debt <= 140; debt = debt * 1. 1) printf (" Your debt is now $ %.2 f\n", debt ); return 0; } 65
for for Your debt is now $100.00 Your debt is now $110.00 Your debt is now $121.00 Your debt is now $133.10 66
for for 6 // for_wild.c: # include <stdio.h> int main ( void ) { int x; int y = 55; for ( x = 1; y <= 75; y = (++ x * 5) + 50) printf (" %10 d %10 d\n", x, y); return 0; } 67
for for 1 55 2 60 3 65 4 70 5 75 68
for for 7 69
for for // for_none.c: # include <stdio.h> int main ( void ) { int ans, n; ans = 2; for ( n = 3; ans <= 25; ) ans = ans * n; printf ("n = %d; ans = %d.\n", n, ans ); return 0; } 70
for for n = 3; ans = 54. 71
for for for ( ; ; ) printf ("I want som action \n"); 72
for for 8 printf 73
for for // for_show.c: # include <stdio.h> int main ( void ) { int num ; for ( printf (" Keep entering numbers!\n"); num!=6; ) scanf ("%d", & num ); printf (" That s the one I want!\n"); return 0; } 74
for for Keep entering numbers! 2 6 That s the one I want! 75
for for 9 for ( n = 1; n < 10000; n = n + delta ) delta if delta 76
+= -= *= /= %= 77
num += 20 num -= 20 num /= 20 num *= 20 num %= 20 num = num + 20 num = num - 20 num = num / 20 num = num * 20 num = num % 20 78
num += 20 num -= 20 num /= 20 num *= 20 num %= 20 num = num + 20 num = num - 20 num = num / 20 num = num * 20 num = num % 20 x *= 3 * y + 12 x = x * (3 * y + 12) 78
= xxxxyyyyzzzz *= 3 xxxxyyyyzzzz = xxxxyyyyzzzz * 3 79
for for 80
1 37 1 23 81
1 // postage.c: 2 # include <stdio.h> 3 int main ( void ) 4 { 5 const int FIRST_ OZ = 37, NEXT_ OZ = 23; 6 int ounces, cost ; 7 for ( ounces = 1, cost = FIRST_ OZ ; 8 ounces <= 16; 9 ounces ++, cost += NEXT_ OZ ) 10 printf ("%2d $ %4.2 f\n", ounces, cost /100.0) ; 11 return 0; 12 } 82
1 $0.37 2 $0.60 3 $0.83... 83
ounces = 1, cost = FIRST_OZ; no ounces <= 16 ounces++, cost += NEXT_OZ yes printf("..."); 84
for for 85
ounces ++, cost = ounces * FIRST_ OZ 86
x = (y = 3, (z = ++y + 2) + 5); (1) y = 3; (2) y = y + 1 = 4; (3) z = ( y + 2) = (4 + 2) = 6; (4) x = z + 5 = 6 + 5 = 11; 87
houseprice = 249, 500; houseprice = 249; 500; 88
houseprice = 249, 500; houseprice = 249; 500; C houseprice = 249 500 500 houseprice 249 88
houseprice = ( 249, 500) ; 89
houseprice = ( 249, 500) ; 500 houseprice 89
int m, n; printf ("%d %d\n", m, n); 90
int m, n; printf ("%d %d\n", m, n); 90
S = 1 + 1 2 + 1 4 + 1 8 + 1 16 + 91
// zeno.c: # include <stdio.h> int main ( void ) { int count, limit ; double sum, x; printf (" Enter the number of terms : "); scanf ("%d", & limit ); for ( sum = 0., x = 1, count = 1; count <= limit ; count ++, x *= 2.) { sum += 1.0/ x; printf (" sum = %f when terms = %d.\n", sum, count ); } return 0; } 92
Enter the number of terms you want : 10 sum = 1. 000000 when terms = 1. sum = 1. 500000 when terms = 2. sum = 1. 750000 when terms = 3. sum = 1. 875000 when terms = 4. sum = 1. 937500 when terms = 5. sum = 1. 968750 when terms = 6. sum = 1. 984375 when terms = 7. sum = 1. 992188 when terms = 8. sum = 1. 996094 when terms = 9. sum = 1. 998047 when terms = 10. 93
(do while)
(do while) while for do while 94
(do while) // do_while.c: # include <stdio.h> int main ( void ) { const int SECRET_ CODE = 13; int code_ entered ; do { printf ("To withdraw money from ATM. \n"); printf (" Enter the secret code number : "); scanf ("%d", & code_entered ); } while ( code_ entered!= SECRET_ CODE ); printf (" Congratulations! You are permitted!\ n" ); return 0; } 95
(do while) To withdraw money from ATM. Please enter the secret code number : 11 To withdraw money from ATM. Please enter the secret code number : 12 To withdraw money from ATM. Please enter the secret code number : 13 Congratulations! You are permitted! 96
(do while) i while // entry.c: # include <stdio.h> int main ( void ) { const int SECRET_ CODE = 13; int code_ entered ; printf ("To withdraw money from ATM. \n"); printf (" Enter the secret code number : "); scanf ("%d", & code_entered ); while ( code_ entered!= SECRET_ CODE ) { printf ("To withdraw money from ATM. \n"); printf (" Enter the secret code number : "); scanf ("%d", & code_entered ); 97
(do while) ii } } printf (" Congratulations! You are permitted!\ n" ); return 0; 98
(do while) do printf("ba la ba la!"); while no go to next statement count++ <= limit; yes 99
(do while) do { statements } while ( condition ); do statement while ( condition ); 100
(do while) do while do while 101
102
for while for (; condition ; ) while ( condition ) 103
while for initialization ; while ( condition ) { body ; update ; } for ( initialization ; condition ; update ){ body ; } 104
for while while while ( scanf ("%ld", & num ) == 1){... } for for ( count = 1; count <= 100; count ++) {... } 105
(nested loop)
(nested loop) 106
(nested loop) // rows1.c: # include <stdio.h> # define ROWS 6 # define CHARS 10 int main ( void ) { int row ; char ch; for ( row = 0; row < ROWS ; row ++) { for (ch = A ; ch < ( A + CHARS ); ch ++) printf ("%c ", ch); printf ("\n"); } return 0; } 107
(nested loop) A B C D E F G H I J A B C D E F G H I J A B C D E F G H I J A B C D E F G H I J A B C D E F G H I J A B C D E F G H I J 108
(nested loop) // row2.c: # include <stdio.h> # define ROWS 6 # define CHARS 6 int main ( void ) { int row ; char ch; for ( row = 0; row < ROWS ; row ++) { for (ch = A + row ; ch < ( A + CHARS ); ch ++) printf ("%c ", ch); printf ("\n"); } return 0; } 109
(nested loop) A B C D E F B C D E F C D E F D E F E F F 110
111
float debts [20]; debts 20 float debts[0] debts[1]... float debts [3] = 2.; debts [7] = 1.2 e +10; 112
scanf ("%d", & debts [4]) ; // 5 113
C debts [20] = 11.0; // debts [31] = 22.22; // 114
int num [10]; // 10 char ch [20]; // 20 double a [40]; // 40 double 115
(subscript) (index) (offset) 0 116
for 5 117
i // scores.c: # include <stdio.h> # define DAYS 5 # define PAR 10000 int main ( void ) { int index, steps [ DAYS ]; int sum = 0; float average ; printf (" Enter steps of %d days :\n", DAYS ); for ( index = 0; index < DAYS ; index ++) scanf ("%d", & steps [ index ]); printf (" The steps read in are as follows :\ n"); for ( index = 0; index < DAYS ; index ++) printf ("%7d", steps [ index ]); printf ("\n"); 118
ii } for ( index = 0; index < DAYS ; index ++) sum += steps [ index ]; average = ( float ) sum / DAYS ; printf (" Sum of steps = %d, average = %.2 f\ n", sum, average ); printf (" That s a handicap of %.0 f.\n", average - PAR ); return 0; 119
Enter steps of 5 days : 9999 9995 11000 12012 11145 95667 The steps read in are as follows : 9999 9995 11000 12012 11145 Sum of steps = 54151, average = 10830. 20 That s a handicap of 830. 120
n p 121
n p math.h pow 121
pow 1 // pow.c: 2 # include <stdio.h> 3 # include <math.h> 4 int main ( void ) 5 { 6 printf (" 2^3 = %f\n", pow (2.0,3.0) ); 7 printf (" sqrt 2 = %f\n", pow (2.0,0.5) ); 8 printf (" 3^(1/4) = %f\n", pow (3.0,0.25) ); 9 return 0; 10 } 122
pow 2^3 = 8. 000000 sqrt 2 = 1. 414214 3^( 1/ 4) = 1. 316074 123
1 double power ( double n, int p) 2 { 3 double pow = 1; 4 int i; 5 for (i = 1; i <= p; i ++) 6 pow *= n; 7 return pow ; 8 } 124
return 125
return return 2 * x + b; 126
b = power (1.2, 3); b = 2.0 + power (1.2, 3); printf ("%f", power (1.2, 3)); 127
power i // power.c: # include <stdio.h> double power ( double n, int p); int main ( void ) { double x, xpow ; int exp ; printf (" Enter a number and the positive integer power \n"); printf (" Enter q to quit.\n"); while ( scanf ("%lf%d", &x, & exp ) == 2) { xpow = power (x, exp ); printf (" %.3 g to the power %d is %.5 g\n", x, exp, xpow ); printf (" Enter next pair of numbers or q to quit.\ n "); 128
power ii } printf (" Hope you enjoyed this power trip -- bye!\ n") ; return 0; } double power ( double n, int p) { double pow = 1; int i; for ( i = 1; i <= p; i ++) pow *= n; return pow ; } 129
power Enter a number and the positive integer power to which the number will be raised. Enter qto quit. 1.2 12 1.2 to the power 12 is 8. 9161 Enter next pair of numbers or q to quit. 2 16 2 to the power 16 is 65536 Enter next pair of numbers or q to quit. q 130
26 26 26 131
// ex01.c # include < stdio.h> # define N 26 int main ( void ) { int i; char alpha [N]; for (i = 0; i < N; i ++) { alpha [i] = a +i; putchar ( alpha [i]); } putchar ( \n ); return 0; } 132
$ $$ $$$ $$$$ $$$$$ 133
// ex02.c: # include <stdio.h> # define SYMBOL $ # define ROW 5 int main ( void ) { int i, j; for (i = 0; i < ROW ; i ++) { for (j = 0; j <= i; j ++) putchar ( SYMBOL ); putchar ( \n ); } return 0; } 134
3 F FE FED FEDC FEDCB FEDCBA 135
// ex03.c: # include <stdio.h> # define ROW 6 int main ( void ) { int i, j; for (i = 0; i < ROW ; i ++) { for (j = 0; j <= i; j ++) putchar ( F -j); putchar ( \n ); } return 0; } 136
4 A ABA ABCBA ABCDCBA ABCDEDCBA 137
// ex04.c: # include < stdio.h> # define ROW 5 int main ( void ) { int i, j; for (i = 0; i < ROW ; i ++) { for (j = ROW -1 -i; j >= 0; j - -) putchar ( ); for (j = 0; j <= i; j ++) putchar ( A +j); for (j = 0; j < i; j ++) putchar ( A +i -1 -j); putchar ( \n ); } return 0; 138
} 139
5 for 140
// ex05.c: # include <stdio.h> int main ( void ) { int i, low, high ; printf (" Enter low and high limit : "); scanf ("%d %d", &low, & high ); printf ("%4s %4s %4s\n", "n", "n^2", "n^3"); for ( i = low ; i <= high ; i ++) printf ("%4d %4d %4d\n", i, i*i, i*i*i); return 0; } 141
6 142
// ex06.c: # include < stdio.h> # include < string.h> int main ( void ) { int i; char word [20]; puts (" Enter a word : "); scanf ("%s", word ); for (i = strlen ( word ) -1; i >= 0; i - -) putchar ( word [i]); putchar ( \n ); return 0; } 143
7 144
// ex07.c # include < stdio.h> int main ( void ) { float a, b; puts (" Enter the first pair of float numbers : " ); while ( scanf ("%f %f", &a, &b) == 2) { printf (" %8.3 f %8.3 f\n", a-b, a*b); puts (" Enter the next pair of float number : " ); } puts (" Done!"); return 0; } 145
8 146
9 Enter lower and upper integer limit : 5 9 The sum of the squares from 25 to 81 is 255 Enter lower and upper integer limit : 3 25 The sum of the squares from 9 to 625 is 5520 Enter lower and upper integer limit : 5 5 Done 147
// ex09.c # include <stdio.h> int main ( void ) { int i, low, high ; int sum = 0; printf (" Enter lower and higher limit : "); while ( scanf ("%d %d", &low, & high ) == 2 && ( low <= high )) { for ( i = low ; i <= high ; i ++) sum += i* i; printf (" The sum of the squares from % d to % d is % d \n", low, high, sum ); } puts (" Done "); return 0; } 148
10 8 149
// ex10.c # include < stdio.h> # define N 8 int main ( void ) { int arr [N]; int i; puts (" Enter 8 integers : "); for (i = 0; i < N; i ++) scanf ("%d", & arr [i]); for (i = N -1; i >= 0; i - -) printf ("%4d ", arr [i]); putchar ( \n ); return 0; } 150
11 1 + 1 2 + 1 3 + 1 4 + + 1 n + 1 1 2 + 1 3 1 4 + +( 1)n+1 1 n + n n = 20,100,500 151
// ex11.c # include < stdio.h> int main ( void ) { int i, n; int symbol = -1; double s1 = 0.0, s2 = 0.0; puts (" Enter the value of n:"); scanf ("%d", &n); for (i = 1; i <= n; i ++) { s1 += 1.0 / i; symbol *= -1; s2 += ( float ) symbol / i; } printf ("s1 = %.5 f when n = %d\n", s1, n); printf ("s2 = %.5 f when n = %d\n", s2, n); return 0; } 152
12 8 int 2 8 for do while 153
// ex12.c # include < stdio.h> # define N 8 int main ( void ) { int i; int arr [N]; for (i = 1, arr [0] = 2; i < N; i ++) arr [i] = arr [i -1]*2; i = 0; do { printf ("%4d\n", arr [i ++]) ; } while (i<n); return 0; } 154
13 8 double 8 i i 155
// ex13.c # include < stdio.h> # define N 4 int main ( void ) { int i; double sum = 0. 0; double arr1 [N], arr2 [N]; printf (" Enter %d double elements : ", N); for (i = 0; i < N; i ++) { scanf ("%lf", & arr1 [i]); sum += arr1 [i]; arr2 [i] = sum ; } for (i = 0; i < N; i ++) { printf (" %8.3 f", arr1 [i]); } 156
} putchar ( \n ); for (i = 0; i < N; i ++) { printf (" %8.3 f", arr2 [i]); } putchar ( \n ); return 0; 157
14 158
// ex14.c # include < stdio.h> # include < string.h> # define N 255 int main ( void ) { char line [N]; char ch; int i = 0; puts (" Enter a line text : "); while (( ch = getchar ())!= \n ) line [i ++] = ch; line [i]= \0 ; for (i = strlen ( line ) -1; i >= 0; i - -) putchar ( line [i]); putchar ( \n ); return 0; } 159
15 10% 10000 5% 10000 160
// ex15.c # include < stdio.h> # define INVEST 1000 # define RATE1 0.1 # define RATE2 0.05 int main ( void ) { int year ; double wang, li; wang = li = INVEST ; for ( year = 1; li <= wang ; year ++) { wang += INVEST * RATE1 ; li *= (1+ RATE2 ); printf ("%4d %8.3 f %8.3 f\n", year, wang, li); } return 0; } 161
16 100 8% 10 162
// ex16.c # include < stdio.h> # define LOTTERY 100. 0 # define RATE 0. 08 int main ( void ) { int year ; double wang = LOTTERY ; for ( year = 1; wang >= 0. 0; year ++) { wang *= (1+ RATE ); wang -= 10. 0; printf ("%4d %8.3 f\n", year, wang ); } return 0; } 163