Successfully reported this slideshow.
Upcoming SlideShare
×

# Cs 1114 - lecture-10

584 views

Published on

• Full Name
Comment goes here.

Are you sure you want to Yes No
• sir do you have next lectures...???

Are you sure you want to  Yes  No
• Be the first to like this

### Cs 1114 - lecture-10

1. 1. Programming Fundamentals
2. 2. Test Yourself <ul><li>Find sum of all integers between any two numbers </li></ul><ul><ul><li>How many times you have to repeat it? </li></ul></ul>Programming Fundamentals | Lecture-10
3. 3. Consider this Example <ul><li>Find even and odd numbers from first n natural numbers </li></ul><ul><ul><li>What’s different in this example? </li></ul></ul><ul><ul><ul><li>Let’s have a look at the solutions of finding even and odd numbers separately </li></ul></ul></ul>Programming Fundamentals | Lecture-10
4. 4. Find first n even numbers Programming Fundamentals | Lecture-9 START count = count + 1 STOP count < n count = 0 Yes No DISPLAY even even = 0 even = even + 2 All odd numbers are skipped by this solution
5. 5. Find first n odd numbers Programming Fundamentals | Lecture-9 START count = count + 1 STOP count < n count = 0 Yes No DISPLAY odd odd = -1 odd = odd + 2 All even numbers are skipped by this solution
6. 6. What’s Required <ul><li>We have to consider all odd and even numbers up to n </li></ul><ul><ul><li>It is quite obvious that our solution needs to decide whether a number is even or odd </li></ul></ul><ul><ul><ul><li>Hence, we’ll use a decision box while finding a number in every repetition </li></ul></ul></ul><ul><ul><ul><ul><li>What should be the decision rule? Or how can we decide that a number that is even or odd? </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>If we divide a number by 2 and remainder is 0 (the number is divisible by 2), it is considered as an even number </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>How can we check the remainder of a division? </li></ul></ul></ul></ul></ul>Programming Fundamentals | Lecture-10
7. 7. Remainder Operator <ul><li>An operator like “+”, “-”, “/” etc. denoted by “%”, that performs the division between two number and gives you the remainder </li></ul><ul><ul><li>Examples: </li></ul></ul><ul><ul><ul><li>4%2 will give 0 as answer because remainder of 4/2 is “0” </li></ul></ul></ul><ul><ul><ul><li>9%2 will give you “1” </li></ul></ul></ul><ul><ul><ul><li>11%3 = ? </li></ul></ul></ul><ul><ul><ul><li>54%5 = ? </li></ul></ul></ul>Programming Fundamentals | Lecture-10
8. 8. Programming Fundamentals | Lecture-10 START count = count + 1 STOP count < n count = 0 No No DISPLAY count “is odd” READ n count % 2 == 0 DISPLAY count “is even” Yes Yes
9. 9. Important Observation <ul><li>Any kind of decision making can be done while repeatedly doing a task </li></ul><ul><ul><li>It can range from simple decision making to complex series of decision makings as you learnt in your previous lessons </li></ul></ul>Programming Fundamentals | Lecture-10
10. 10. Try this Yourself <ul><li>Find all numbers which are divisible by 3 from first n integers </li></ul><ul><li>Find all numbers which are divisible by a number from first n integers </li></ul>Programming Fundamentals | Lecture-10
11. 11. Programming Fundamentals | Lecture-10 START count = count + 1 STOP count < n count = 0 Yes No READ n count % 3 == 0 DISPLAY count Yes No
12. 12. Consider this Example <ul><li>Find all odd numbers which are divisible by 3 from first n integers </li></ul><ul><ul><li>How many decisions are required in every repetition? </li></ul></ul>Programming Fundamentals | Lecture-10
13. 13. Programming Fundamentals | Lecture-10 START count = count + 1 STOP count < n count = 0 Yes No READ n Yes No count % 2 != 0 count % 3 == 0 DISPLAY count Yes No
14. 14. Consider this Example <ul><li>Display “multiplication table” of a number only if the number is not equal to “0” </li></ul>Programming Fundamentals | Lecture-10
15. 15. Programming Fundamentals | Lecture-10 START count = count + 1 STOP count < n count = 0 Yes No DISPLAY num * count READ num, n num != 0 No Yes No
16. 16. Important Observation <ul><li>“Decision Making” and “Repetitions” can be inter-mixed in either way </li></ul><ul><ul><li>Repeatedly doing a task if a certain condition is true OR </li></ul></ul><ul><ul><li>While repeatedly doing a task, making certain decisions OR </li></ul></ul><ul><ul><li>Both of the above </li></ul></ul>Programming Fundamentals | Lecture-10
17. 17. A Premonition <ul><li>All of you who are not yet practicing well enough and taking this course “lightly and non-seriously” should: </li></ul><ul><ul><li>Get ready to face hard-time (as things are going to get worse for them) OR </li></ul></ul><ul><ul><li>Start practicing before it gets too late </li></ul></ul>Programming Fundamentals | Lecture-10
18. 18. Tasks (to be done by next lecture) <ul><li>Display “Fibonacci Series” up to n </li></ul><ul><li>Display “Fibonacci Series” up to n, if n is not less than 0 </li></ul>Programming Fundamentals | Lecture-10
19. 19. Programming Fundamentals | Lecture-10 BE PREPARED FOR QUIZ IN NEXT LECTURE