Understanding Programming – A Different Approach
- We have created a recipe and compared that to scripting or programming
-
We have 3 essentials
- ingredients
- Steps to follow
- Equipment
-
Today lets continue that discussion
- Lets try to create a recipe for making a tea/ginger tea
Ingredients:
Milk
tea powder
water
ginger (if necessary)
sugar
Equipment:
1. Bowl
2. Stove
3. Cups to serve
4. strainer
Steps:
1. Add water to bowl
2. Add tea powder
3. Add sugar
4. Add milk
5. Add ginger (if necessary)
6. Wait till the tea boils thrice
7. filter and serve into cups
- We have if necessary
-
skipping certain steps based on outcomes is referred as conditonal statements in programming.
-
Exercise: Create a recipe to make coffee for 2 people each person should get 200 ml of hot coffee
Problem – To find whether the number is even or odd
ingredient:
number
steps:
1. check if number ends with 0,2,4,6,8
2. if yes even no otherwise
Problem – To find sum of all numbers till given number
5 => 5+4+3+2+1 => 15
ingredient:
number
steps:
1. calculate number * (number+ 1)/2
or
steps:
1. start from 1 and add the numbers till number (1+2+...+number)
Problem
- If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
- Try to write a recipe to find all the numbers below 25 that are multiples of 3 or 5