1 Summary

  • Several toy examples for beginners

2 Calculate the sum of two integers a and b.

  • There is only one “main” function
  • Remember to declare function “add” if it is behind the fuction that calls it. No requirement of the declaration otherwise.
  • Note the form of function’s declaration

3 Calculate partial sum of number series

3.1 Calculate the series of odds, sum = +3+5+7+…+20

  • There is a key word “while” to deal with loop
  • Think about how to break or early break the loop

3.2 Calculate the sum of factorials, sum = 1!+2!+3!+…+8!

  • There is a key word “for” to deal with loop
  • Think about how to break or early break the loop

4 Think about how to approximate \(\sin(x)\).

  • It can be written as the series \(\sin(x) = x-x^3/3!+x^5/5!-x^7/7!+\cdots\). Thus, we can approximate \(\sin(x)\) by partial sum of this sequence with a given precision, say \(1e-6\).