What is an algorithm?
Algorithm can be defined as step by step procedure to solve a given problem by using a computer system to get desired result. It includes finite number of steps in hierarchical order from start to stop.
Characteristics of algorithm:
1. It should be unambiguous (no doubts).
2. It should not have any uncertainty steps.
3. It should generate correct result.
Examples.
Algorithm to check for palindrome.
Step1: Start
Step2: Read a number i.e. num
Step3: dup=num
Step4: while (num!=0)
rem=num%10
rev=(rev*10)+rem
num=num/10
Step5: Print reversed number i.e. rev
Step6: if (rev==dup)
Display "Plaidrome"
else
Display "Not Palidrome"
Step7 : Stop
Algorithm to generate electricity bill
Step1: Start
Step2: Read user name and number of units consumed i.e. name, units
Step3: if (units<=200) goto next step; otherwise goto step 5
Step4: amt=units*0.80 and goto step
Step5: if (units<=300) then goto next step; otherwise goto step 7
Step6: amt=160+((units-200)*0.90) and goto step 8
Step7: amt=250+((units-300)+1) and goto next step
Step8: total_amt=amt+100
Step9: if (total_amt>400) goto next step; otherwise goto step 11
Step10: sur_charge=total_amt*0.15, total_amt=total_amt+sur_charge
Step11: Print name and total_amt
Step12: Stop
Algorithm can be defined as step by step procedure to solve a given problem by using a computer system to get desired result. It includes finite number of steps in hierarchical order from start to stop.
Characteristics of algorithm:
1. It should be unambiguous (no doubts).
2. It should not have any uncertainty steps.
3. It should generate correct result.
Examples.
Algorithm to check for palindrome.
Step1: Start
Step2: Read a number i.e. num
Step3: dup=num
Step4: while (num!=0)
rem=num%10
rev=(rev*10)+rem
num=num/10
Step5: Print reversed number i.e. rev
Step6: if (rev==dup)
Display "Plaidrome"
else
Display "Not Palidrome"
Step7 : Stop
Algorithm to generate electricity bill
Step1: Start
Step2: Read user name and number of units consumed i.e. name, units
Step3: if (units<=200) goto next step; otherwise goto step 5
Step4: amt=units*0.80 and goto step
Step5: if (units<=300) then goto next step; otherwise goto step 7
Step6: amt=160+((units-200)*0.90) and goto step 8
Step7: amt=250+((units-300)+1) and goto next step
Step8: total_amt=amt+100
Step9: if (total_amt>400) goto next step; otherwise goto step 11
Step10: sur_charge=total_amt*0.15, total_amt=total_amt+sur_charge
Step11: Print name and total_amt
Step12: Stop
No comments:
Post a Comment