summaryrefslogtreecommitdiff
path: root/Practical_C_Programming/Chapter_8_4.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Practical_C_Programming/Chapter_8_4.ipynb')
-rw-r--r--Practical_C_Programming/Chapter_8_4.ipynb252
1 files changed, 234 insertions, 18 deletions
diff --git a/Practical_C_Programming/Chapter_8_4.ipynb b/Practical_C_Programming/Chapter_8_4.ipynb
index f7a42df6..24580c55 100644
--- a/Practical_C_Programming/Chapter_8_4.ipynb
+++ b/Practical_C_Programming/Chapter_8_4.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 8"
+ "name": "",
+ "signature": "sha256:10a432341f6033fcf678cc580d953b0c94ad7350aa1431ba3e0517904d3e90c8"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -11,25 +12,46 @@
"cell_type": "heading",
"level": 1,
"metadata": {},
- "source": "Chapter 8: More control statements"
+ "source": [
+ "Chapter 8: More control statements"
+ ]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 8.1, Page number: 144"
+ "source": [
+ "Example 8.1, Page number: 144"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 8.1.py\n# To calculate the sum of numbers entered by the user\n\n\n# Variable declaration\ntotal = 0\ncounter = 0\n\n# Calculation\nwhile (counter < 5) :\n current = 3\n total += current\n counter += 1\n\n# Result\nprint ('The grand total is %d' % total)",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "total = 0\n",
+ "counter = 0\n",
+ "\n",
+ "# Calculation\n",
+ "while (counter < 5) :\n",
+ " current = 3\n",
+ " total += current\n",
+ " counter += 1\n",
+ "\n",
+ "# Result\n",
+ "print ('The grand total is %d' % total)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The grand total is 15\n"
+ "text": [
+ "The grand total is 15\n"
+ ]
}
],
"prompt_number": 1
@@ -38,19 +60,35 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 8.2, Page number: 145"
+ "source": [
+ "Example 8.2, Page number: 145"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 8.2.py\n# To calculate the sum of numbers entered by the user using for loop\n\n\n# Variable declaration\ntotal = 0\n\n# Calculation\nfor counter in range (0, 5) :\n current = 5\n total += current\n\n# Result\nprint ('The grand total is %d' % total)",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "total = 0\n",
+ "\n",
+ "# Calculation\n",
+ "for counter in range (0, 5) :\n",
+ " current = 5\n",
+ " total += current\n",
+ "\n",
+ "# Result\n",
+ "print ('The grand total is %d' % total)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The grand total is 25\n"
+ "text": [
+ "The grand total is 25\n"
+ ]
}
],
"prompt_number": 2
@@ -59,19 +97,129 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 8.3, Page number: 146"
+ "source": [
+ "Example 8.3, Page number: 146"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 8.3.py\n# To produce a Celsius to Fahrenheit conversion chart for the numbers 0 to 100\n\n\n# Variable declaration, calculation and result\nfor celsius in range (0, 101) :\n print ('Celsius: %d Fahrenheit: %d' % (celsius, (celsius * 9) / 5 +32))",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration, calculation and result\n",
+ "for celsius in range (0, 101) :\n",
+ " print ('Celsius: %d Fahrenheit: %d' % (celsius, (celsius * 9) / 5 +32))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Celsius: 0 Fahrenheit: 32\nCelsius: 1 Fahrenheit: 33\nCelsius: 2 Fahrenheit: 35\nCelsius: 3 Fahrenheit: 37\nCelsius: 4 Fahrenheit: 39\nCelsius: 5 Fahrenheit: 41\nCelsius: 6 Fahrenheit: 42\nCelsius: 7 Fahrenheit: 44\nCelsius: 8 Fahrenheit: 46\nCelsius: 9 Fahrenheit: 48\nCelsius: 10 Fahrenheit: 50\nCelsius: 11 Fahrenheit: 51\nCelsius: 12 Fahrenheit: 53\nCelsius: 13 Fahrenheit: 55\nCelsius: 14 Fahrenheit: 57\nCelsius: 15 Fahrenheit: 59\nCelsius: 16 Fahrenheit: 60\nCelsius: 17 Fahrenheit: 62\nCelsius: 18 Fahrenheit: 64\nCelsius: 19 Fahrenheit: 66\nCelsius: 20 Fahrenheit: 68\nCelsius: 21 Fahrenheit: 69\nCelsius: 22 Fahrenheit: 71\nCelsius: 23 Fahrenheit: 73\nCelsius: 24 Fahrenheit: 75\nCelsius: 25 Fahrenheit: 77\nCelsius: 26 Fahrenheit: 78\nCelsius: 27 Fahrenheit: 80\nCelsius: 28 Fahrenheit: 82\nCelsius: 29 Fahrenheit: 84\nCelsius: 30 Fahrenheit: 86\nCelsius: 31 Fahrenheit: 87\nCelsius: 32 Fahrenheit: 89\nCelsius: 33 Fahrenheit: 91\nCelsius: 34 Fahrenheit: 93\nCelsius: 35 Fahrenheit: 95\nCelsius: 36 Fahrenheit: 96\nCelsius: 37 Fahrenheit: 98\nCelsius: 38 Fahrenheit: 100\nCelsius: 39 Fahrenheit: 102\nCelsius: 40 Fahrenheit: 104\nCelsius: 41 Fahrenheit: 105\nCelsius: 42 Fahrenheit: 107\nCelsius: 43 Fahrenheit: 109\nCelsius: 44 Fahrenheit: 111\nCelsius: 45 Fahrenheit: 113\nCelsius: 46 Fahrenheit: 114\nCelsius: 47 Fahrenheit: 116\nCelsius: 48 Fahrenheit: 118\nCelsius: 49 Fahrenheit: 120\nCelsius: 50 Fahrenheit: 122\nCelsius: 51 Fahrenheit: 123\nCelsius: 52 Fahrenheit: 125\nCelsius: 53 Fahrenheit: 127\nCelsius: 54 Fahrenheit: 129\nCelsius: 55 Fahrenheit: 131\nCelsius: 56 Fahrenheit: 132\nCelsius: 57 Fahrenheit: 134\nCelsius: 58 Fahrenheit: 136\nCelsius: 59 Fahrenheit: 138\nCelsius: 60 Fahrenheit: 140\nCelsius: 61 Fahrenheit: 141\nCelsius: 62 Fahrenheit: 143\nCelsius: 63 Fahrenheit: 145\nCelsius: 64 Fahrenheit: 147\nCelsius: 65 Fahrenheit: 149\nCelsius: 66 Fahrenheit: 150\nCelsius: 67 Fahrenheit: 152\nCelsius: 68 Fahrenheit: 154\nCelsius: 69 Fahrenheit: 156\nCelsius: 70 Fahrenheit: 158\nCelsius: 71 Fahrenheit: 159\nCelsius: 72 Fahrenheit: 161\nCelsius: 73 Fahrenheit: 163\nCelsius: 74 Fahrenheit: 165\nCelsius: 75 Fahrenheit: 167\nCelsius: 76 Fahrenheit: 168\nCelsius: 77 Fahrenheit: 170\nCelsius: 78 Fahrenheit: 172\nCelsius: 79 Fahrenheit: 174\nCelsius: 80 Fahrenheit: 176\nCelsius: 81 Fahrenheit: 177\nCelsius: 82 Fahrenheit: 179\nCelsius: 83 Fahrenheit: 181\nCelsius: 84 Fahrenheit: 183\nCelsius: 85 Fahrenheit: 185\nCelsius: 86 Fahrenheit: 186\nCelsius: 87 Fahrenheit: 188\nCelsius: 88 Fahrenheit: 190\nCelsius: 89 Fahrenheit: 192\nCelsius: 90 Fahrenheit: 194\nCelsius: 91 Fahrenheit: 195\nCelsius: 92 Fahrenheit: 197\nCelsius: 93 Fahrenheit: 199\nCelsius: 94 Fahrenheit: 201\nCelsius: 95 Fahrenheit: 203\nCelsius: 96 Fahrenheit: 204\nCelsius: 97 Fahrenheit: 206\nCelsius: 98 Fahrenheit: 208\nCelsius: 99 Fahrenheit: 210\nCelsius: 100 Fahrenheit: 212\n"
+ "text": [
+ "Celsius: 0 Fahrenheit: 32\n",
+ "Celsius: 1 Fahrenheit: 33\n",
+ "Celsius: 2 Fahrenheit: 35\n",
+ "Celsius: 3 Fahrenheit: 37\n",
+ "Celsius: 4 Fahrenheit: 39\n",
+ "Celsius: 5 Fahrenheit: 41\n",
+ "Celsius: 6 Fahrenheit: 42\n",
+ "Celsius: 7 Fahrenheit: 44\n",
+ "Celsius: 8 Fahrenheit: 46\n",
+ "Celsius: 9 Fahrenheit: 48\n",
+ "Celsius: 10 Fahrenheit: 50\n",
+ "Celsius: 11 Fahrenheit: 51\n",
+ "Celsius: 12 Fahrenheit: 53\n",
+ "Celsius: 13 Fahrenheit: 55\n",
+ "Celsius: 14 Fahrenheit: 57\n",
+ "Celsius: 15 Fahrenheit: 59\n",
+ "Celsius: 16 Fahrenheit: 60\n",
+ "Celsius: 17 Fahrenheit: 62\n",
+ "Celsius: 18 Fahrenheit: 64\n",
+ "Celsius: 19 Fahrenheit: 66\n",
+ "Celsius: 20 Fahrenheit: 68\n",
+ "Celsius: 21 Fahrenheit: 69\n",
+ "Celsius: 22 Fahrenheit: 71\n",
+ "Celsius: 23 Fahrenheit: 73\n",
+ "Celsius: 24 Fahrenheit: 75\n",
+ "Celsius: 25 Fahrenheit: 77\n",
+ "Celsius: 26 Fahrenheit: 78\n",
+ "Celsius: 27 Fahrenheit: 80\n",
+ "Celsius: 28 Fahrenheit: 82\n",
+ "Celsius: 29 Fahrenheit: 84\n",
+ "Celsius: 30 Fahrenheit: 86\n",
+ "Celsius: 31 Fahrenheit: 87\n",
+ "Celsius: 32 Fahrenheit: 89\n",
+ "Celsius: 33 Fahrenheit: 91\n",
+ "Celsius: 34 Fahrenheit: 93\n",
+ "Celsius: 35 Fahrenheit: 95\n",
+ "Celsius: 36 Fahrenheit: 96\n",
+ "Celsius: 37 Fahrenheit: 98\n",
+ "Celsius: 38 Fahrenheit: 100\n",
+ "Celsius: 39 Fahrenheit: 102\n",
+ "Celsius: 40 Fahrenheit: 104\n",
+ "Celsius: 41 Fahrenheit: 105\n",
+ "Celsius: 42 Fahrenheit: 107\n",
+ "Celsius: 43 Fahrenheit: 109\n",
+ "Celsius: 44 Fahrenheit: 111\n",
+ "Celsius: 45 Fahrenheit: 113\n",
+ "Celsius: 46 Fahrenheit: 114\n",
+ "Celsius: 47 Fahrenheit: 116\n",
+ "Celsius: 48 Fahrenheit: 118\n",
+ "Celsius: 49 Fahrenheit: 120\n",
+ "Celsius: 50 Fahrenheit: 122\n",
+ "Celsius: 51 Fahrenheit: 123\n",
+ "Celsius: 52 Fahrenheit: 125\n",
+ "Celsius: 53 Fahrenheit: 127\n",
+ "Celsius: 54 Fahrenheit: 129\n",
+ "Celsius: 55 Fahrenheit: 131\n",
+ "Celsius: 56 Fahrenheit: 132\n",
+ "Celsius: 57 Fahrenheit: 134\n",
+ "Celsius: 58 Fahrenheit: 136\n",
+ "Celsius: 59 Fahrenheit: 138\n",
+ "Celsius: 60 Fahrenheit: 140\n",
+ "Celsius: 61 Fahrenheit: 141\n",
+ "Celsius: 62 Fahrenheit: 143\n",
+ "Celsius: 63 Fahrenheit: 145\n",
+ "Celsius: 64 Fahrenheit: 147\n",
+ "Celsius: 65 Fahrenheit: 149\n",
+ "Celsius: 66 Fahrenheit: 150\n",
+ "Celsius: 67 Fahrenheit: 152\n",
+ "Celsius: 68 Fahrenheit: 154\n",
+ "Celsius: 69 Fahrenheit: 156\n",
+ "Celsius: 70 Fahrenheit: 158\n",
+ "Celsius: 71 Fahrenheit: 159\n",
+ "Celsius: 72 Fahrenheit: 161\n",
+ "Celsius: 73 Fahrenheit: 163\n",
+ "Celsius: 74 Fahrenheit: 165\n",
+ "Celsius: 75 Fahrenheit: 167\n",
+ "Celsius: 76 Fahrenheit: 168\n",
+ "Celsius: 77 Fahrenheit: 170\n",
+ "Celsius: 78 Fahrenheit: 172\n",
+ "Celsius: 79 Fahrenheit: 174\n",
+ "Celsius: 80 Fahrenheit: 176\n",
+ "Celsius: 81 Fahrenheit: 177\n",
+ "Celsius: 82 Fahrenheit: 179\n",
+ "Celsius: 83 Fahrenheit: 181\n",
+ "Celsius: 84 Fahrenheit: 183\n",
+ "Celsius: 85 Fahrenheit: 185\n",
+ "Celsius: 86 Fahrenheit: 186\n",
+ "Celsius: 87 Fahrenheit: 188\n",
+ "Celsius: 88 Fahrenheit: 190\n",
+ "Celsius: 89 Fahrenheit: 192\n",
+ "Celsius: 90 Fahrenheit: 194\n",
+ "Celsius: 91 Fahrenheit: 195\n",
+ "Celsius: 92 Fahrenheit: 197\n",
+ "Celsius: 93 Fahrenheit: 199\n",
+ "Celsius: 94 Fahrenheit: 201\n",
+ "Celsius: 95 Fahrenheit: 203\n",
+ "Celsius: 96 Fahrenheit: 204\n",
+ "Celsius: 97 Fahrenheit: 206\n",
+ "Celsius: 98 Fahrenheit: 208\n",
+ "Celsius: 99 Fahrenheit: 210\n",
+ "Celsius: 100 Fahrenheit: 212\n"
+ ]
}
],
"prompt_number": 3
@@ -80,19 +228,46 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 8.4, Page number: 147"
+ "source": [
+ "Example 8.4, Page number: 147"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 8.4.py\n# To count the number of 3's and 7's in an array\n\n\n# Variable declaration\nseven_count = 0\nthree_count = 0\ndata = []\n\n# Calculation\nfor i in range (0, 5) :\n x = 7\n data.append(int(x))\nprint (data)\n\nfor index in range (0, 5) :\n if data[index] == 3 :\n three_count += 1\n\n if data[index] == 7 :\n seven_count += 1\n\n# Result\nprint ('Threes %d Sevens %d' % (three_count, seven_count))",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "seven_count = 0\n",
+ "three_count = 0\n",
+ "data = []\n",
+ "\n",
+ "# Calculation\n",
+ "for i in range (0, 5) :\n",
+ " x = 7\n",
+ " data.append(int(x))\n",
+ "print (data)\n",
+ "\n",
+ "for index in range (0, 5) :\n",
+ " if data[index] == 3 :\n",
+ " three_count += 1\n",
+ "\n",
+ " if data[index] == 7 :\n",
+ " seven_count += 1\n",
+ "\n",
+ "# Result\n",
+ "print ('Threes %d Sevens %d' % (three_count, seven_count))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "[7, 7, 7, 7, 7]\nThrees 0 Sevens 5\n"
+ "text": [
+ "[7, 7, 7, 7, 7]\n",
+ "Threes 0 Sevens 5\n"
+ ]
}
],
"prompt_number": 4
@@ -101,19 +276,60 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 8.6, Page number: 149"
+ "source": [
+ "Example 8.6, Page number: 149"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 8.6.py\n# To perform various arithmetic operations based on operator input by the user\n\n\n# Variable declaration\nresult = 0\ni = 0\n\n# Calculation and result\nwhile (i < 3) :\n print ('Result: %d' % result)\n operator = '-'\n value = 10\n\n if operator == 'q' or operator == 'Q' :\n break\n\n elif operator == '+' :\n result += value\n\n elif operator == '-' :\n result -= value\n\n elif operator == '*' :\n result *= value\n\n elif operator == '/' :\n if value == 0 :\n print ('Error: Divide by zero')\n print ('operation ignored') \n else :\n result /= value\n\n else :\n print ('Unknown operator %c' % operator)\n i = i + 1",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "result = 0\n",
+ "i = 0\n",
+ "\n",
+ "# Calculation and result\n",
+ "while (i < 3) :\n",
+ " print ('Result: %d' % result)\n",
+ " operator = '-'\n",
+ " value = 10\n",
+ "\n",
+ " if operator == 'q' or operator == 'Q' :\n",
+ " break\n",
+ "\n",
+ " elif operator == '+' :\n",
+ " result += value\n",
+ "\n",
+ " elif operator == '-' :\n",
+ " result -= value\n",
+ "\n",
+ " elif operator == '*' :\n",
+ " result *= value\n",
+ "\n",
+ " elif operator == '/' :\n",
+ " if value == 0 :\n",
+ " print ('Error: Divide by zero')\n",
+ " print ('operation ignored') \n",
+ " else :\n",
+ " result /= value\n",
+ "\n",
+ " else :\n",
+ " print ('Unknown operator %c' % operator)\n",
+ " i = i + 1"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Result: 0\nResult: -10\nResult: -20\n"
+ "text": [
+ "Result: 0\n",
+ "Result: -10\n",
+ "Result: -20\n"
+ ]
}
],
"prompt_number": 5
@@ -121,7 +337,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []