Latihan

15
Latihan

description

Latihan. Buat algoritma untuk menampilkan output berikut : * * * * *. Buat algoritma untuk menampilkan tulisan berikut : iteration number 1 iteration number 2 iteration number 3 iteration number 4 iteration number 5 after loop ends. - PowerPoint PPT Presentation

Transcript of Latihan

Page 1: Latihan

Latihan

Page 2: Latihan

• Buat algoritma untuk menampilkan output berikut:*****

Page 3: Latihan

• Buat algoritma untuk menampilkan tulisan berikut:

iteration number 1iteration number 2iteration number 3iteration number 4iteration number 5after loop ends

Page 4: Latihan

• Buat algoritma untuk menampilkan output berikut

student no-1 is : student no-2 is : student no-3 is :

Page 5: Latihan

• Buat algoritma untuk menerima inputan tiga buah bilangan real, lalu menghitung rata-ratanya, dan menampilkan hasilnya ke layar

Page 6: Latihan

Write the algorithm to calculate cost of electricitywith following constraints :

(KWHs) Rate 0 - 500 2000501 - 1000 2000 + 0.03 * per KWH over 1001 3500 + 0.02 * per KWH

Enter the values of previous month meters, current month meter, and then displays the total cost which would exist.

Page 7: Latihan

7

The structure of the FOR-TO-DO statement

FOR counter variable initial_value TO final_value program statement

ENDFOR

Page 8: Latihan

8

Flowchart FOR-TO-DO

No

yes

STARTSTART

ENDEND

Counter >= initial_value AND

counter <= final_value ?

Counter >= initial_value AND

counter <= final_value ?

actionaction

{initialize}{initialize}

Page 9: Latihan

9

Example 1

Pseudocode

FOR x1 TO 5 DO

output (x)

ENDFOR

Flowchart

x >=1 AND x <= 5 ?

x >=1 AND x <= 5 ?

START

ENDoutput (x)

No

yes

Page 10: Latihan

10

Example 2

PseudocodeOUTPUT('before loop starts') FOR i = 1 TO 5 DO

OUTPUT('iteration number ',i) ENDFOR OUTPUT('after loop ends')

Page 11: Latihan

11

Execution

• generates the output

Iteration no.

i i >=1 AND

i<= 5 ?

Output

before loop starts

1 1 True iteration number 1

2 2 True iteration number 2

3 3 True iteration number 3

4 4 True iteration number 4

5 5 True iteration number 5

6 6 False after loop ends

Page 12: Latihan

12

Example 3

FOR i 1 TO 5 DO Output (‘*’) {action statement}

ENDFOR {end loop}The result of program :

*****

Page 13: Latihan

13

Example 4• Program with enter the value of 3 student’s scores :

Pseudocode

FOR i 1 TO 3 DOOutput (‘Score student no-‘,i,’ is:’)Input (score)

ENDFOR• Execution :

– Score student no-1 is : __– Score student no-2 is : __– Score student no-3 is : __

Page 14: Latihan

14

Example 5

• Average of 3 numbersnumber 0 {initialize}FOR i 1 TO 3 DO

Input (x) {input value of x}number number + x {sum of x variables}

ENDFOR average number/ 3 {calculate the average}Output (average)

Page 15: Latihan