diff options
Diffstat (limited to 'The_Spirit_of_C/chapter3.ipynb')
-rwxr-xr-x | The_Spirit_of_C/chapter3.ipynb | 241 |
1 files changed, 241 insertions, 0 deletions
diff --git a/The_Spirit_of_C/chapter3.ipynb b/The_Spirit_of_C/chapter3.ipynb new file mode 100755 index 00000000..b498486d --- /dev/null +++ b/The_Spirit_of_C/chapter3.ipynb @@ -0,0 +1,241 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:c466b513c45bfe09da1b0507614f538770318fb66aadf4da4e1995363df32911" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 3 , Integer Arithmetic Expressions" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 3-1 , Page number: 33" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# An introduction to arithmetic \n", + "\n", + "a = 2\n", + "b = 3\n", + "c = 4\n", + "\n", + "answer = a + b\n", + "print \"a + b = %d\"%answer\n", + "\n", + "answer = a - b\n", + "print \"a - b = %d\"%answer\n", + "\n", + "answer = a * b\n", + "print \"a * b = %d\"%answer\n", + "\n", + "answer = a / b\n", + "print \"a / b = %d\"%answer\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a + b = 5\n", + "a - b = -1\n", + "a * b = 6\n", + "a / b = 0\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 3-2 , Page number: 34" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Illustration of mathematical hierarchy\n", + "\n", + "a = 2\n", + "b = 3\n", + "c = 4\n", + "\n", + "answer = a + b * c\n", + "\n", + "print \"a = %d, b = %d, c= %d\"%(a,b,c)\n", + "print \"a + b * c = %d\\n\"%answer" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a = 2, b = 3, c= 4\n", + "a + b * c = 14\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 3-3 , Page number: 35" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# A more advanced illustration of mathematical hierarchy\n", + "\n", + "a = 5\n", + "b = 10\n", + "c = 15\n", + "\n", + "answer = a + b / c\n", + "print \"%d + %d / %d = %d\"%(a,b,c,answer)\n", + "\n", + "answer = b * c - a\n", + "print \"%d * %d - %d = %d\"%(a,b,c,answer)\n", + "\n", + "answer = a * b / c\n", + "print \"%d * %d / %d = %d\"%(a,b,c,answer)\n", + "\n", + "answer = (a + c) * b / a\n", + "print \"(%d + %d) * %d / %d = %d\"%(a,c,b,a,answer)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "5 + 10 / 15 = 5\n", + "5 * 10 - 15 = 145\n", + "5 * 10 / 15 = 3\n", + "(5 + 15) * 10 / 5 = 40\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 3-4 , Page number: 37" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Illustration of modulus operator\n", + "\n", + "a = 4\n", + "b = 8\n", + "c = 13\n", + "\n", + "print \"%d mod %d = %d\"%(b,a,b%a)\n", + "print \"%d mod %d = %d\"%(c,a,c%a)\n", + "print \"%d mod %d = %d\"%(c,b,c%b)\n", + "\n", + "print \"The modulus operator is: %\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "8 mod 4 = 0\n", + "13 mod 4 = 1\n", + "13 mod 8 = 5\n", + "The modulus operator is: %\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Program 3-5 , Page number: 38" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Illustration of the unary minus\n", + "\n", + "a = -4\n", + "b = 5\n", + "c = -6\n", + "\n", + "print \"a = %d, b = %d, c = %d\\n\\n\"%(a,b,c)\n", + "print \" and now\\n\\n\"\n", + "\n", + "a = -a\n", + "b = -b\n", + "c = -c\n", + "\n", + "print \"a = %d, b = %d, c = %d\\n\\n\"%(a,b,c)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a = -4, b = 5, c = -6\n", + "\n", + "\n", + " and now\n", + "\n", + "\n", + "a = 4, b = -5, c = 6\n", + "\n", + "\n" + ] + } + ], + "prompt_number": 5 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |