Monday, September 15, 2014

[Delivery] Exercise While

WHILE 

Bài 1: Declare a variable which has the age of the person. Print the user’s name as many times as his age


My Code + result

Bài 2: The program displays even numbers from 1 to 30.


My Code + result


Bài 3: The program displays numbers from 10 to 0 in the reverse order.


My Code + result

Bài 4: The program will accept integers and display them until zero (0) is entered


My Code + result

Bài 5: Find the factorial of a number.


 Hint:
  1. Get the number.
  2. To begin, set the factorial of the number to be one.
  3. While the number is greater than one
  4. Set the factorial to be the factorial multiplied by the number.
  5. Decrement the number.
  6. Print out the factorial

My Code + result

Bài 6:  Write a program to print the series 100, 95 , 90, 85,………., 5.


My Code + result

Bài 7: Accept two numbers num1 and num2. Find the sum of all odd numbers between the two numbers entered .



My Code + result

Bài 8: Write a program to generate the Fibonacci series. (1,1,2,3,5,8,13,………)


My Code + result

Bài học rút ra:


  • Cấu trúc while (condition) {statements)
  • Thay vì viết a = a + b ta có thể viết a += b
  • Thay vì viết a = a - 1 hay a = a + 1 ta có thể viết a-- hay a++ để giảm hay tăng lên 1 đơn vị

No comments:

Post a Comment