Sunday, September 14, 2014

[Delivery] More if

Bài 1: Write a program that accepts two numbers a and b and checks whether or not a is divisible by b.


Flow Chart: 

Flow Chart



My Code


  • Bước 1: Đặt biến a, b.
  • Bước 2: Dùng printf()scanf() để người dùng nhập số.
  • Bước 3: Dùng lệnh if
    • Nếu a % b == 0 thì in It is divisible.
    • Nếu không thì in It is not divisible.
Final result:
Divisible


Not divisible






Bài 2: Write a program to accept 2 numbers and tell whether the product of the two numbers is equal to or greater than 1000.


Flow Chart: 

My Code
  • Bước 1: Đặt biến a, b.
  • Bước 2: Dùng printf()scanf() để người dùng nhập số.
  • Bước 3: Dùng lệnh if
    • Nếu a * b == 1000 thì in The product is EQUAL 1000.
    • Nếu không thì in The product is NOT EQUAL 1000.

Final result:
EQUAL 1000

NOT EQUAL 1000


Bài 3: Write a program to accept 2 numbers. Calculate the difference between the two values. If the difference is equal to any of the values entered, then display the following message :


Difference is equal to value <number of value entered>
If the difference is not equal to any of the values entered, display the following message:
Difference is not equal to any of the values entered

Flow Chart:

Flow Chart

My Code
  • Bước 1: Đặt biến a, b.
  • Bước 2: Dùng printf()scanf() để người dùng nhập số.
  • Bước 3: Đặt biến c và dùng printf() để xem giá trị a - b.
  • Bước 4: Dùng if
    • Dùng if nếu condition a - b == a thì in Diference is equal to value a.
    • Dùng else if nếu a - b == b thì in Diference is equal to value b.
    • Dùng else khi a - b không bằng giá trị nào thì in Difference is not equal to any values entered.
Final result:
Difference equal value b


Difference not equal any values entered



Bài 4: Montek company gives allowances to its employees depending on their grade as follows :

  
Grade                                 Allowance
  A                                        300
  B                                        250
     Others                                100       
Calculate the salary at the end of the month.   (Accept Salary and Grade from the user )

Flow Chart:


My Code
  • Bước 1: Đặt biến float salary và char grade
  • Bước 2: Dùng printf()scanf() để người dùng nhập salary và grade
  • Bước 3: Dùng if
    • Dùng if nếu condition grade =='a' || grade == 'A' thì có salary = salary + 300.
    • Dùng else if nếu grade =='b' || grade == 'B' thì có salary = salary + 250.
    • Dùng else khi grade không bằng giá trị nào thì có salary = salary + 100.
Final result:

salary = 100, grade = b


Bài 5: Write a program to evaluate the Grade of a student for the following constraints

If marks >75 – grade A
If 60< marks < 75 – grade B
If 45<marks<60 – grade C
If 35<marks<45 - grade D
If marks < 35 – grade E

Flow Chart:

My Code



  • Bước 1: Đặt biến int mark;
  • Bước 2: Dùng printf()scanf() để người dùng nhập điểm (mark).
  • Bước 3: Dùng if
    • Dùng if nếu condition mark > 75 thì in Grade A.
    • Dùng else if với condition mark > 60 && mark < 75 thì in Grade B
    • ...............
Final result:




Bài học rút ra:


  • Cách dùng cấu trúc if
    • if ( condition ) { statement } else if ( condition 2 ) { statement 2 } .......
  • || là chỉ hoặc cái này đúng , hoặc cái kia đúng
  • && là chỉ cái này và cái kia đều đúng

No comments:

Post a Comment