diff options
Diffstat (limited to 'Practical_C_Programming/Chapter_6_4.ipynb')
-rw-r--r-- | Practical_C_Programming/Chapter_6_4.ipynb | 148 |
1 files changed, 134 insertions, 14 deletions
diff --git a/Practical_C_Programming/Chapter_6_4.ipynb b/Practical_C_Programming/Chapter_6_4.ipynb index 22907862..2769b901 100644 --- a/Practical_C_Programming/Chapter_6_4.ipynb +++ b/Practical_C_Programming/Chapter_6_4.ipynb @@ -1,6 +1,7 @@ { "metadata": { - "name": "Chapter 6" + "name": "", + "signature": "sha256:72c49f1aed7f4d43ac1084e67245cfba7b011cb43f84b1ab3ef13b081aec557f" }, "nbformat": 3, "nbformat_minor": 0, @@ -11,25 +12,56 @@ "cell_type": "heading", "level": 1, "metadata": {}, - "source": "Chapter 6: Decision and control statements" + "source": [ + "Chapter 6: Decision and control statements" + ] }, { "cell_type": "heading", "level": 3, "metadata": {}, - "source": "Example 6.1, Page number: 114" + "source": [ + "Example 6.1, Page number: 114" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Example 6.1.py\n# To print the Fibonacci series upto 100\n\n\n# Variable declaration\nold_number = 1\ncurrent_number = 1\n\nprint ('1')\n\n# Calculation and result\nwhile (current_number < 100) :\n print (current_number)\n next_number = current_number + old_number\n\n old_number = current_number\n current_number = next_number", + "input": [ + "\n", + "# Variable declaration\n", + "old_number = 1\n", + "current_number = 1\n", + "\n", + "print ('1')\n", + "\n", + "# Calculation and result\n", + "while (current_number < 100) :\n", + " print (current_number)\n", + " next_number = current_number + old_number\n", + "\n", + " old_number = current_number\n", + " current_number = next_number" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "1\n1\n2\n3\n5\n8\n13\n21\n34\n55\n89\n" + "text": [ + "1\n", + "1\n", + "2\n", + "3\n", + "5\n", + "8\n", + "13\n", + "21\n", + "34\n", + "55\n", + "89\n" + ] } ], "prompt_number": 1 @@ -38,19 +70,52 @@ "cell_type": "heading", "level": 3, "metadata": {}, - "source": "Example 6.2, Page number: 115" + "source": [ + "Example 6.2, Page number: 115" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Example 6.2.py\n# To calculate the number of items entered\n\n\n# Variable declaration\ntotal = 0\ni = 0\n\n# Calculation\nwhile (i < 10) :\n item = 1\n\n if item == 0 :\n break\n\n total += item\n print ('Total: %d' % total)\n i = i + 1\n \n# Result\nprint ('Final total: %d' % total)", + "input": [ + "\n", + "# Variable declaration\n", + "total = 0\n", + "i = 0\n", + "\n", + "# Calculation\n", + "while (i < 10) :\n", + " item = 1\n", + "\n", + " if item == 0 :\n", + " break\n", + "\n", + " total += item\n", + " print ('Total: %d' % total)\n", + " i = i + 1\n", + " \n", + "# Result\n", + "print ('Final total: %d' % total)" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Total: 1\nTotal: 2\nTotal: 3\nTotal: 4\nTotal: 5\nTotal: 6\nTotal: 7\nTotal: 8\nTotal: 9\nTotal: 10\nFinal total: 10\n" + "text": [ + "Total: 1\n", + "Total: 2\n", + "Total: 3\n", + "Total: 4\n", + "Total: 5\n", + "Total: 6\n", + "Total: 7\n", + "Total: 8\n", + "Total: 9\n", + "Total: 10\n", + "Final total: 10\n" + ] } ], "prompt_number": 1 @@ -59,19 +124,59 @@ "cell_type": "heading", "level": 3, "metadata": {}, - "source": "Example 6.3, Page number: 116" + "source": [ + "Example 6.3, Page number: 116" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Example 6.3.py\n# To calculate the total number of items entered and omit the negative items\n\n\n# Variable declaration\ntotal = 0\nminus_items = 0\ni = 0\n\n# Calculation\nwhile (i < 10) :\n item = 1\n \n if item == 0 :\n break\n\n if item < 0 :\n minus_items += 1\n continue\n\n total += item\n print ('Total: %d' % total)\n i = i + 1\n \n# Result\nprint ('Final total: %d' % total)\nprint ('with %d negative items omitted' % minus_items)", + "input": [ + "\n", + "# Variable declaration\n", + "total = 0\n", + "minus_items = 0\n", + "i = 0\n", + "\n", + "# Calculation\n", + "while (i < 10) :\n", + " item = 1\n", + " \n", + " if item == 0 :\n", + " break\n", + "\n", + " if item < 0 :\n", + " minus_items += 1\n", + " continue\n", + "\n", + " total += item\n", + " print ('Total: %d' % total)\n", + " i = i + 1\n", + " \n", + "# Result\n", + "print ('Final total: %d' % total)\n", + "print ('with %d negative items omitted' % minus_items)" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Total: 1\nTotal: 2\nTotal: 3\nTotal: 4\nTotal: 5\nTotal: 6\nTotal: 7\nTotal: 8\nTotal: 9\nTotal: 10\nFinal total: 10\nwith 0 negative items omitted\n" + "text": [ + "Total: 1\n", + "Total: 2\n", + "Total: 3\n", + "Total: 4\n", + "Total: 5\n", + "Total: 6\n", + "Total: 7\n", + "Total: 8\n", + "Total: 9\n", + "Total: 10\n", + "Final total: 10\n", + "with 0 negative items omitted\n" + ] } ], "prompt_number": 2 @@ -80,19 +185,34 @@ "cell_type": "heading", "level": 3, "metadata": {}, - "source": "Example 6.4, Page number: 118" + "source": [ + "Example 6.4, Page number: 118" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Example 6.4.py\n# To calculate the number of dollars owed\n\n\n# Variable declaration\nbalance_owed = 100\n\n# Calculation and result\nif balance_owed == 0 :\n print ('You owe nothing.')\nelse :\n print ('You owe %d dollars.' % balance_owed)", + "input": [ + "\n", + "\n", + "# Variable declaration\n", + "balance_owed = 100\n", + "\n", + "# Calculation and result\n", + "if balance_owed == 0 :\n", + " print ('You owe nothing.')\n", + "else :\n", + " print ('You owe %d dollars.' % balance_owed)" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "You owe 100 dollars.\n" + "text": [ + "You owe 100 dollars.\n" + ] } ], "prompt_number": 3 |