Chapter 04 Statement and Control Flow


1.What is a statement ?

2. if ... else ...

3.if ... elseif ... elseif ...

4.switch ... case ... default

5.nested if


Exercise :

1.Write a program which can accept an input number, and judge whether the number is even or odd, then print the result.

For example,

Please input a number : 5
5 is an odd number.

2.Write a program which can accept a number, and judge whether the number is positive or negative. If the number equals zero, then print "zero".

For example,

Please input a number: -2
-2 is negative.

Please input a number: 3
3 is positive.

Please input a number: 0
0 is zero.

3.Write a program which can accept a number between 0 to 9, and print the English form of the number. If the number is greater then 9 or less then 0, print "Your input is illegal !".

For example,

Please input a number between 0 to 9 : 2
Your input is two

Please input a number between 0 to 9 : 8
Your input is eight

Please input a number between 0 to 9 : -1
Your input is illegal !


Advanced Exercise:

1.Write a program which can accept a positve number, and print all the even number less than the input number.

2.Write a program which can accept a positive number, n , and print the result of 1+2+3+...+n.


1.For

2.Do While

3.Break and Continue

4.Nested loop


Exercise:

1.請寫一程式,輸入一個正整數 n,輸出為 1,2,3,...,n

2.請寫一程式,輸入一個正整數 n,計算小於等於 n 的所有偶數的和。

3.請寫一程式,輸入一個正整數 n,列印出小於等於 n 的所有質數。

4.請寫一程式,輸入兩個正整數 a 和 b,求出 a 與 b 的最大公因數 (gcd) 與最小公倍數 (lcm)。

5.數列 a(0)=1, a(1)=1, a(2)=2, a(3)=3, a(4)=5, ..., a(n)=a(n-1)+a(n-2),試寫一程式,可輸入一正整數 n ,然後印出費氏數列 1,...,n的所有值。


 

Last update : December 27, 2000