Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Algorithm…. 2 Algorithm: Is a planned set of steps to solve certain problem Ex.: Assume for instance that the gross pay of an employee is to be calculated.

Similar presentations


Presentation on theme: "1 Algorithm…. 2 Algorithm: Is a planned set of steps to solve certain problem Ex.: Assume for instance that the gross pay of an employee is to be calculated."— Presentation transcript:

1 1 Algorithm…

2 2 Algorithm: Is a planned set of steps to solve certain problem Ex.: Assume for instance that the gross pay of an employee is to be calculated and then 10 percent of the gross is to remove as tax. The yield being the net pay. Write down the algorithm for this problem. Sol.: 1. Begin 2. input name, hours_worked, and wage par hour 3. calculate gross_pay = hours_worked * wage par hour 4. calculate tax = (10/100) * gross_pay 5. calculate net_pay = gross_pay – tax 6. print name, net_pay 7. End

3 3 Algorithm: Ex.: Write an algorithm to read the name and the mark (one) for one student then add to his mark 5 marks Sol.: 1. Begin 2. input name, mark 3. new_mark = mark+5 4. print name, new_mark 5. End

4 4 Algorithm: Ex.: Write an algorithm to read the name and mark of student and calculate the new mark of each by adding 5 to its mark. We have 10 students Sol.: 1. Begin 2. studentcount =1 3. if studentcount > 10 then Stop else A. read name, mark B. calculate new_mark = mark + 5 C. print name, new_mark D. studentcount = studentcount +1 E. goto step 3

5 5 Pseudo code algorithm: It is an algorithm written in a language that is very close to high level languages while …do loop syntax: while ( condition) do begin end ex.: 1. start 2. student_count = 1 3. while (student_count < = 10) do 4. begin 5. read name, mark 6. new_mark = mark + 5 7. print name, new_mark 8. student_count = student_count + 1 9. end 10. stop

6 6 Pseudo code algorithm: repeat … until loop syntax: repeat until (condition); ex.: 1. start 2. student_count = 1 3. repeat 4. read name, mark 5. new_mark = mark + 5 6. print name, new_mark 7. student_count = student_count + 1 8. until (student_count > 10); 9. stop

7 7 Pseudo code algorithm: For loop syntax: for variable_name = initial_value to end_value do begin end ex.: 1. start 2. for student_count = 1 to 10 do 3. begin 4. read name, mark 5. new_mark = mark + 5 6. print name, new_mark 7. end 8. stop

8 8 Pseudo code algorithm: ex.: write an algorithm to compute the sum of n numbers sol.: 1. start 2. read n 3. sum = 0 4. for I = 1 to n 5. begin 6. sum = sum + I 7. end 8. write sum 9. stop

9 9 Pseudo code algorithm: ex.: write an algorithm to compute y= 1+ 22/2 + 33/3 + … + nn/n sol.: 1. start 2. read n 3. sum = 0 4. for I = 1 to n do 5. begin 6. sum = sum + (I ** I) / I 7. end 8. y = sum 9. print y 10. stop

10 10 Pseudo code algorithm: if statement: syntax: if ( condition) then begin end or: if ( condition) then begin end else begin end or: if ( condition) then begin end else if ( condition) then begin end or: if ( condition) then else

11 11 Pseudo code algorithm: Ex.: Write an algorithm to find the maximum of two values Sol.: 1. Start 2. Read a, b 3. if ( a>b) then 4. max = a 5. else 6. if ( b>a) then 7. max = b 8. else 9. print (a = b) 10. print max 11. Stop

12 12 Pseudo code algorithm: Ex.: Write an algorithm to find the maximum of three values Sol.: 1. Start 2. Read a, b, c 3. if ( a>b) then 4. if ( a>c) then 5. max = a 6. else 7. if ( b>c) then 8. max = b 9. else 10. max = c 11. else 12. if ( b>c) then 13. max = b 14. else 15. max = c 16. print max 17. Stop

13 13 Pseudo code algorithm: ex.: write an algorithm to compute y= 1- 22/2 + 33/3 - … nn/n sol.: 1. start 2. read n 3. sum = 0 4. for I = 1 to n do 5. begin 6. if ( I mod 2 = 0) then 7. sum = sum - (I ** I) / I 8. else 9. sum = sum + (I ** I) / I 10. end 11. y = sum 12. print y 13. stop


Download ppt "1 Algorithm…. 2 Algorithm: Is a planned set of steps to solve certain problem Ex.: Assume for instance that the gross pay of an employee is to be calculated."

Similar presentations


Ads by Google