C/C++ 语言 - 循环

Similar documents
C

C/C++ - 字符输入输出和字符确认

C/C++ - 函数

C/C++语言 - 运算符、表达式和语句

C/C++程序设计 - 字符串与格式化输入/输出

C/C++ - 文件IO

C/C++语言 - 分支结构

C/C++语言 - C/C++数据

C

新版 明解C言語入門編

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

C/C++ - 字符串与字符串函数

C 1

untitled

新・明解C言語入門編『索引』

新・解きながら学ぶC言語

( CIP) /. :, ( ) ISBN TP CIP ( 2005) : : : : * : : 174 ( A ) : : ( 023) : ( 023)

C/C++ - 数组与指针

CC213

C C

3.1 num = 3 ch = 'C' 2

CC213

nooog

Microsoft Word - 第3章.doc

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf ("%d", & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf ("%d %d

C/C++ - 结构体、共用体、枚举体

CC213

1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf ("%d", & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9

Ps22Pdf

CHAPTER VC#

C

Microsoft Word - CPE考生使用手冊 docx

untitled

プログラムの設計と実現II

2013 C 1 #include <stdio.h> 2 int main(void) 3 { 4 int cases, i; 5 long long a, b; 6 scanf("%d", &cases); 7 for (i = 0; i < cases; i++) 8 { 9 scanf("%

C++ 程式設計

untitled

FY.DOC

epub 33-8

新・解きながら学ぶJava

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.07.doc

Microsoft Word - C-pgm-ws2010.doc

untitled

《C语言程序设计》教材习题参考答案

untitled

1 LINUX IDE Emacs gcc gdb Emacs + gcc + gdb IDE Emacs IDE C Emacs Emacs IDE ICE Integrated Computing Environment Emacs Unix Linux Emacs Emacs Emacs Un

2015年计算机二级(C语言)模拟试题及答案(四)

ebook39-5

高中英文科教師甄試心得

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

四川省普通高等学校

Microsoft Word - 09.數學 docx

3. 給 定 一 整 數 陣 列 a[0] a[1] a[99] 且 a[k]=3k+1, 以 value=100 呼 叫 以 下 兩 函 式, 假 設 函 式 f1 及 f2 之 while 迴 圈 主 體 分 別 執 行 n1 與 n2 次 (i.e, 計 算 if 敘 述 執 行 次 數, 不

<5B BECBB0EDB8AEC1F25D312D34B0AD5FC3E2BCAEBCF6BEF7C0DAB7E F31702E504446>

Microsoft Word - 圣经释经学

Gerotor Motors Series Dimensions A,B C T L L G1/2 M G1/ A 4 C H4 E

新版 明解C++入門編

PowerPoint Presentation

, 7, Windows,,,, : ,,,, ;,, ( CIP) /,,. : ;, ( 21 ) ISBN : -. TP CIP ( 2005) 1

Microsoft Word - 095_ 什麼最快樂 (白話與經文加註)-ok .doc

PIC_SERVER (11) SMTP ( ) ( ) PIC_SERVER (10) SMTP PIC_SERVER (event driven) PIC_SERVER SMTP 1. E-

Microsoft Word 軟體設計第二部份範例試題_C++_ _1_.doc

untitled

家长讲师团案例之一

51 C 51 isp 10 C PCB C C C C KEIL

c_cpp

CHAPTER 1

概述

WWW PHP Comments Literals Identifiers Keywords Variables Constants Data Types Operators & Expressions 2

攝 心 為 戒, 因 戒 而 生 定, 由 定 而 生 慧


untitled

Lorem ipsum dolor sit amet, consectetuer adipiscing elit

PowerPoint プレゼンテーション

Microsoft Word doc

Microsoft PowerPoint - 01_Introduction.ppt

Microsoft Word - 正文.doc

6 C51 ANSI C Turbo C C51 Turbo C C51 C51 C51 C51 C51 C51 C51 C51 C C C51 C51 ANSI C MCS-51 C51 ANSI C C C51 bit Byte bit sbit

untitled

数据结构与算法 - Python基础

( 含 要 ) 1-2 用 或 雇 用, 抑 或 有 無 俸 給 文 職 或 武 職, 政 官 或 事 官 均 屬 之, 其 不 以 具 備 人 資 格 為 限, 因 此 屬 於 最 廣 義 之 念 四 廣 義 念 之 依 服 24 條 之 規 定 : 本 於 受 有 俸 給 之 文 武 職, 及

02

Microsoft Word - unitmtg09.doc

101_

,,,,,,,,,, ( http: \ \ www. ncre. cn,, ) 30,,,,,,,, C : C : : 19 : : : /16 : : 96 : : : ISBN 7

Python a p p l e b e a r c Fruit Animal a p p l e b e a r c 2-2

Ps22Pdf

Microsoft PowerPoint - OPVB1基本VB.ppt

Microsoft PowerPoint - ds-1.ppt [兼容模式]

Microsoft Word - 澎湖田調報告_璉謙組.doc


_汪_文前新ok[3.1].doc

TX-NR3030_BAS_Cs_ indd

untitled


chap07.key

华恒家庭网关方案

untitled

download.kaoyan.com_2006ÄêÌì½ò¹¤Òµ´óѧ¸ß¼¶ÓïÑÔ³ÌÐòÉè¼Æ£¨409£©¿¼ÑÐÊÔÌâ

Transcription:

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