summaryrefslogtreecommitdiff
path: root/Programming_in_C/Chapter_13.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Programming_in_C/Chapter_13.ipynb')
-rw-r--r--Programming_in_C/Chapter_13.ipynb201
1 files changed, 201 insertions, 0 deletions
diff --git a/Programming_in_C/Chapter_13.ipynb b/Programming_in_C/Chapter_13.ipynb
new file mode 100644
index 00000000..0ccf61c4
--- /dev/null
+++ b/Programming_in_C/Chapter_13.ipynb
@@ -0,0 +1,201 @@
+{
+ "metadata": {
+ "name": "Chapter XIII"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 13: The preprocessor"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Program 13.1, Page number: 300"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#13.1.py\n",
+ "#defining constants\n",
+ "\n",
+ "#Variable Decarations\n",
+ "global YES\n",
+ "global NO\n",
+ "YES=1\n",
+ "NO=0\n",
+ "\n",
+ "\n",
+ "#Function to check if a number is even or odd\n",
+ "def isEven(number):\n",
+ " global YES\n",
+ " global NO\n",
+ "\n",
+ " if(number%2==0):\n",
+ " answer=YES\n",
+ " else:\n",
+ " answer=NO\n",
+ "\n",
+ " return answer\n",
+ "\n",
+ "#Main()\n",
+ "def main():\n",
+ " global YES\n",
+ " global NO\n",
+ "\n",
+ " #Calculation/Result\n",
+ " if(isEven(17)==YES):\n",
+ " print(\"YES\")\n",
+ " else:\n",
+ " print(\"NO\")\n",
+ "\n",
+ " \n",
+ " if(isEven(20)==YES):\n",
+ " print(\"YES\")\n",
+ " else:\n",
+ " print(\"NO\")\n",
+ "\n",
+ "\n",
+ "#Setting top level conditional script\n",
+ "if __name__=='__main__':\n",
+ " main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "NO\n",
+ "YES\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Program 13.2, Page number: 302"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#13.2.py\n",
+ "#Working more with constants\n",
+ "\n",
+ "global PI\n",
+ "PI= 3.141592654\n",
+ "\n",
+ "\n",
+ "#Function to calculate area\n",
+ "def area(r):\n",
+ " global PI\n",
+ " return (PI*r*r)\n",
+ "\n",
+ "#Function to calculate circumference\n",
+ "def circumference(r):\n",
+ " global PI\n",
+ " return (2*PI*r)\n",
+ "\n",
+ "#Function to calculate volume\n",
+ "def volume(r):\n",
+ " global PI\n",
+ " return (1.33 * PI*r*r*r)\n",
+ " \n",
+ "\n",
+ "#Main()\n",
+ "def main():\n",
+ " print(\"radius=1 {0:5} {1:5} {2:5}\".format(area(1),circumference(1),volume(1)))\n",
+ " print(\"radius=4.98 {0:5} {1:5} {2:5}\".format(area(4.98),circumference(4.98),volume(4.98)))\n",
+ "\n",
+ "\n",
+ "#Setting top level conditional script\n",
+ "if __name__=='__main__':\n",
+ " main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "radius=1 3.141592654 6.283185308 4.17831822982\n",
+ "radius=4.98 77.9127544563 31.2902628338 516.047337866\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Program 13.3, Page number: 314"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#13.3.py\n",
+ "#Defining global constants--advance\n",
+ "\n",
+ "#Variable Declaration\n",
+ "global QUARTS_PER_LITER\n",
+ "QUARTS_PER_LITER=1.05687\n",
+ "\n",
+ "#Main()\n",
+ "def main():\n",
+ " global QUARTS_PER_LITER #Global Reference\n",
+ "\n",
+ " print(\"***Liters to galons***\")\n",
+ " print(\"Enter the number of Litres\")\n",
+ " liters=12 #liters=raw_input()\n",
+ "\n",
+ " gallons=float(liters)*QUARTS_PER_LITER/4.0 #calculation\n",
+ "\n",
+ " print(\"{0} Liters = {1} gallons\".format(liters,gallons))\n",
+ "\n",
+ "#Setting top level conditional script\n",
+ "if __name__=='__main__':\n",
+ " main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "***Liters to galons***\n",
+ "Enter the number of Litres\n",
+ "12 Liters = 3.17061 gallons\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file