SlideShare a Scribd company logo
Programming Fundamentals
Consider this Example Find sum of first 5 natural numbers Programming Fundamentals | Lecture-8
Programming Fundamentals | Lecture-8 START STOP sum = 0 sum = sum + 2 sum = sum + 3 sum = sum + 4 sum = sum + 5 DISPLAY sum (sum=1) (sum=3) (sum=6) (sum=10) (sum=15) sum = sum + 1 (sum=0)
Consider This What if we have to find sum of first 50 natural numbers? Should we write down 50 processing steps? Programming Fundamentals | Lecture-8
Programming Fundamentals | Lecture-8 What if the numbers are not just 50, they are 1000?
Analyze the Problem Notice that you are doing the same operation of addition again and again The only difference is the value which is being added If we could somehow instruct the computer to repeat certain number of operations for certain number of times, our job will be much more easier Programming Fundamentals | Lecture-8
Is this Correct? Programming Fundamentals | Lecture-8 START sum = 0 sum = sum + _ STOP
What is Required? Your program needs to  decide  after every repetition that whether further repetition is required or not? Whether number of repetitions has reached its limit or not? As the program needs to “ decide ”, it is clear that we need to use a “decision box” What will be the basis of decision? Programming Fundamentals | Lecture-8
How to Achieve This? Need to keep record of number of repetitions (repetitions count) Make use of a field whose purpose is to keep record of no. of repetitions which are done so far That field (or repetition count) will become the basis of decision Hence your program will be able to control the repetitions and will proceed further after the desired no. of repetitions are done Programming Fundamentals | Lecture-8
Sum of 1 st  5 natural numbers Programming Fundamentals | Lecture-8 START sum = 0 count = count + 1 STOP count < 5 count = 0 DISPLAY sum Yes No sum = sum + count
How to Test This? Programming Fundamentals | Lecture-8 count = 0 sum = 0 Repetition-1 Repetition-2 Repetition-3 Repetition-4 Repetition-5 decision (count < 5) 0 < 5 1 < 5 2 < 5 3 < 5 4 < 5 count  =  count  + 1 1 2 3 4 5 sum  =  sum  +  count 1 3 6 10 15
Power of Computer Programs Controlled Repetition Programmers can instruct computers to do certain tasks repeatedly for certain period of time As the complexity of problems increases, repetition may become more extensive This way computers prove very useful by performing repeating tasks themselves without the intervention of human beings Programming Fundamentals | Lecture-8
Steps to Follow Which instructions are needed to be repeated? How many times the repetition is to be done? Programming Fundamentals | Lecture-8
Extending that Example Find sum of first n natural numbers Where n can be any number Programming Fundamentals | Lecture-8
Programming Fundamentals | Lecture-8 START sum = 0 count = count + 1 STOP count < n count = 0 DISPLAY sum Yes No sum = sum + count READ n
Another Example Find factorial of  a number factorial(n) = 1 * 2 * …… * n Programming Fundamentals | Lecture-8
Programming Fundamentals | Lecture-8 START fact = 1 count = count + 1 STOP count < n count = 0 DISPLAY sum Yes No fact = fact * count  READ n
Testing Programming Fundamentals | Lecture-8 count = 0 fact = 1 n = 5 120 = 24 * 5 5 = 4 + 1 4 < 5 Repetition-5 Repetition-1 Repetition-2 Repetition-3 Repetition-4 decision (count < 5) 0 < 5 1 < 5 2 < 5 3 < 5 count  =  count  + 1 1 = 0 + 1 2 = 1 + 1 3 = 2 + 1 4 = 3 + 1 fact  =  fact *  count 1 = 1 * 1 2 = 2 *1 6 = 2 * 3 24 = 6 * 4
Try this Yourself Find 2 20 Hint: Multiply 2 by itself, 20 times Find x y x and y can be any numbers X y  is basically equivalent to  x multiplied by itself y times Programming Fundamentals | Lecture-8
Tasks  (to be done by next lecture) Find the sum of square of first n natural numbers Find first 5 multiples of a number Hint: Multiply the number from 1 to 5
Programming Fundamentals | Lecture-8 BE PREPARED FOR  QUIZ IN NEXT LECTURE

More Related Content

PPT
Algorithms and flowcharts ppt (seminar presentation)..
PPTX
Algorithm Development
PPT
Knightstour
PPTX
Std 10 computer chapter 9 Problems and Problem Solving
PDF
4. algorithm
PPTX
PPTX
Lab 1:c++
PDF
Divide&Conquer & Dynamic Programming
Algorithms and flowcharts ppt (seminar presentation)..
Algorithm Development
Knightstour
Std 10 computer chapter 9 Problems and Problem Solving
4. algorithm
Lab 1:c++
Divide&Conquer & Dynamic Programming

What's hot (15)

PPTX
parallelizing Trapezoidal rule
PPT
Model and Design
PPTX
PPTX
Introduction to Dynamic Programming, Principle of Optimality
PPT
Dynamic programming 2
PDF
Exam100118
PPT
Dynamicpgmming
PDF
Exam100412
DOCX
Computer programming
PPTX
Greedy method
PPTX
Dynamic programming
PPTX
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
DOCX
Introduction to programming
PDF
جلسه اول الگوریتم و فلوچارت
PPTX
Introduction to dynamic programming
parallelizing Trapezoidal rule
Model and Design
Introduction to Dynamic Programming, Principle of Optimality
Dynamic programming 2
Exam100118
Dynamicpgmming
Exam100412
Computer programming
Greedy method
Dynamic programming
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
Introduction to programming
جلسه اول الگوریتم و فلوچارت
Introduction to dynamic programming
Ad

Viewers also liked (13)

PPT
Fundamentals of programming angeli
PPTX
Programming Fundamental Slide No.1
PDF
Introduction of Functional Programming
PPTX
Programming fundamentals lecture 1 0f c
PPSX
Programming Fundamental Presentation
PDF
Intro to functional programming
PPT
Fundamental Programming Lect 2
PPT
Fundamental Programming Lect 3
PPT
Programming fundamentals lecture 1&2
PPT
Fundamental Programming Lect 1
PPTX
Programming Fundamentals
PPT
Programing Fundamental
PPTX
Functional Programming Fundamentals
Fundamentals of programming angeli
Programming Fundamental Slide No.1
Introduction of Functional Programming
Programming fundamentals lecture 1 0f c
Programming Fundamental Presentation
Intro to functional programming
Fundamental Programming Lect 2
Fundamental Programming Lect 3
Programming fundamentals lecture 1&2
Fundamental Programming Lect 1
Programming Fundamentals
Programing Fundamental
Functional Programming Fundamentals
Ad

Similar to Cs 1114 - lecture-8 (20)

PPT
Cs 1114 - lecture-9
PPTX
Lecture 7 - Programming Fundamentals.p8ptx
PPT
Cs 1114 - lecture-10
PPT
JavaYDL20
PPTX
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS - SARASWATHI RAMALINGAM
PPT
Cs 1114 - lecture-2
PDF
Stanford splash spring 2016 basic programming
PPT
Algorithms with-java-1.0
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
PPT
L12 complexity
PPTX
CSE 115 l project
PPT
Cs 1114 - lecture-3
PPT
chapter1.ppt
PPTX
Embedded systems
PPTX
Basic pogramming concepts
PPT
Basic_analysis.ppt
PPTX
Lecture 7.pptx
PPT
17recursion
PDF
C++ progrmming language
PPT
tutorial5.ppt
Cs 1114 - lecture-9
Lecture 7 - Programming Fundamentals.p8ptx
Cs 1114 - lecture-10
JavaYDL20
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS - SARASWATHI RAMALINGAM
Cs 1114 - lecture-2
Stanford splash spring 2016 basic programming
Algorithms with-java-1.0
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
L12 complexity
CSE 115 l project
Cs 1114 - lecture-3
chapter1.ppt
Embedded systems
Basic pogramming concepts
Basic_analysis.ppt
Lecture 7.pptx
17recursion
C++ progrmming language
tutorial5.ppt

More from Zeeshan Sabir (8)

PPT
Cs 1114 - lecture-7
PPT
Cs 1114 - lecture-6
PPT
Cs 1114 - lecture-4
PPT
Cs 1114 - lecture-1
PPT
Cs 1114 - lecture-5
PPT
Cs 1114 - lecture-29
PPT
Final logic presentation
PPT
Human resource management process
Cs 1114 - lecture-7
Cs 1114 - lecture-6
Cs 1114 - lecture-4
Cs 1114 - lecture-1
Cs 1114 - lecture-5
Cs 1114 - lecture-29
Final logic presentation
Human resource management process

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Encapsulation theory and applications.pdf
PDF
Approach and Philosophy of On baking technology
PDF
cuic standard and advanced reporting.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Spectroscopy.pptx food analysis technology
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Empathic Computing: Creating Shared Understanding
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
A Presentation on Artificial Intelligence
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
Machine Learning_overview_presentation.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Review of recent advances in non-invasive hemoglobin estimation
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
A comparative analysis of optical character recognition models for extracting...
Encapsulation theory and applications.pdf
Approach and Philosophy of On baking technology
cuic standard and advanced reporting.pdf
Network Security Unit 5.pdf for BCA BBA.
Spectroscopy.pptx food analysis technology
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Encapsulation_ Review paper, used for researhc scholars
Diabetes mellitus diagnosis method based random forest with bat algorithm
Empathic Computing: Creating Shared Understanding
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
A Presentation on Artificial Intelligence
The AUB Centre for AI in Media Proposal.docx
NewMind AI Weekly Chronicles - August'25-Week II
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Machine Learning_overview_presentation.pptx

Cs 1114 - lecture-8

  • 2. Consider this Example Find sum of first 5 natural numbers Programming Fundamentals | Lecture-8
  • 3. Programming Fundamentals | Lecture-8 START STOP sum = 0 sum = sum + 2 sum = sum + 3 sum = sum + 4 sum = sum + 5 DISPLAY sum (sum=1) (sum=3) (sum=6) (sum=10) (sum=15) sum = sum + 1 (sum=0)
  • 4. Consider This What if we have to find sum of first 50 natural numbers? Should we write down 50 processing steps? Programming Fundamentals | Lecture-8
  • 5. Programming Fundamentals | Lecture-8 What if the numbers are not just 50, they are 1000?
  • 6. Analyze the Problem Notice that you are doing the same operation of addition again and again The only difference is the value which is being added If we could somehow instruct the computer to repeat certain number of operations for certain number of times, our job will be much more easier Programming Fundamentals | Lecture-8
  • 7. Is this Correct? Programming Fundamentals | Lecture-8 START sum = 0 sum = sum + _ STOP
  • 8. What is Required? Your program needs to decide after every repetition that whether further repetition is required or not? Whether number of repetitions has reached its limit or not? As the program needs to “ decide ”, it is clear that we need to use a “decision box” What will be the basis of decision? Programming Fundamentals | Lecture-8
  • 9. How to Achieve This? Need to keep record of number of repetitions (repetitions count) Make use of a field whose purpose is to keep record of no. of repetitions which are done so far That field (or repetition count) will become the basis of decision Hence your program will be able to control the repetitions and will proceed further after the desired no. of repetitions are done Programming Fundamentals | Lecture-8
  • 10. Sum of 1 st 5 natural numbers Programming Fundamentals | Lecture-8 START sum = 0 count = count + 1 STOP count < 5 count = 0 DISPLAY sum Yes No sum = sum + count
  • 11. How to Test This? Programming Fundamentals | Lecture-8 count = 0 sum = 0 Repetition-1 Repetition-2 Repetition-3 Repetition-4 Repetition-5 decision (count < 5) 0 < 5 1 < 5 2 < 5 3 < 5 4 < 5 count = count + 1 1 2 3 4 5 sum = sum + count 1 3 6 10 15
  • 12. Power of Computer Programs Controlled Repetition Programmers can instruct computers to do certain tasks repeatedly for certain period of time As the complexity of problems increases, repetition may become more extensive This way computers prove very useful by performing repeating tasks themselves without the intervention of human beings Programming Fundamentals | Lecture-8
  • 13. Steps to Follow Which instructions are needed to be repeated? How many times the repetition is to be done? Programming Fundamentals | Lecture-8
  • 14. Extending that Example Find sum of first n natural numbers Where n can be any number Programming Fundamentals | Lecture-8
  • 15. Programming Fundamentals | Lecture-8 START sum = 0 count = count + 1 STOP count < n count = 0 DISPLAY sum Yes No sum = sum + count READ n
  • 16. Another Example Find factorial of a number factorial(n) = 1 * 2 * …… * n Programming Fundamentals | Lecture-8
  • 17. Programming Fundamentals | Lecture-8 START fact = 1 count = count + 1 STOP count < n count = 0 DISPLAY sum Yes No fact = fact * count READ n
  • 18. Testing Programming Fundamentals | Lecture-8 count = 0 fact = 1 n = 5 120 = 24 * 5 5 = 4 + 1 4 < 5 Repetition-5 Repetition-1 Repetition-2 Repetition-3 Repetition-4 decision (count < 5) 0 < 5 1 < 5 2 < 5 3 < 5 count = count + 1 1 = 0 + 1 2 = 1 + 1 3 = 2 + 1 4 = 3 + 1 fact = fact * count 1 = 1 * 1 2 = 2 *1 6 = 2 * 3 24 = 6 * 4
  • 19. Try this Yourself Find 2 20 Hint: Multiply 2 by itself, 20 times Find x y x and y can be any numbers X y is basically equivalent to x multiplied by itself y times Programming Fundamentals | Lecture-8
  • 20. Tasks (to be done by next lecture) Find the sum of square of first n natural numbers Find first 5 multiples of a number Hint: Multiply the number from 1 to 5
  • 21. Programming Fundamentals | Lecture-8 BE PREPARED FOR QUIZ IN NEXT LECTURE

Editor's Notes

  • #9: * Only a decision box can divide the order or flow of program into two alternative paths