summaryrefslogtreecommitdiff
path: root/Programming_in_C/Chapter_06.ipynb
diff options
context:
space:
mode:
authorJovina Dsouza2014-06-18 12:43:07 +0530
committerJovina Dsouza2014-06-18 12:43:07 +0530
commit206d0358703aa05d5d7315900fe1d054c2817ddc (patch)
treef2403e29f3aded0caf7a2434ea50dd507f6545e2 /Programming_in_C/Chapter_06.ipynb
parentc6f0d6aeb95beaf41e4b679e78bb42c4ffe45a40 (diff)
downloadPython-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.gz
Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.bz2
Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.zip
adding book
Diffstat (limited to 'Programming_in_C/Chapter_06.ipynb')
-rw-r--r--Programming_in_C/Chapter_06.ipynb496
1 files changed, 496 insertions, 0 deletions
diff --git a/Programming_in_C/Chapter_06.ipynb b/Programming_in_C/Chapter_06.ipynb
new file mode 100644
index 00000000..a612ce70
--- /dev/null
+++ b/Programming_in_C/Chapter_06.ipynb
@@ -0,0 +1,496 @@
+{
+ "metadata": {
+ "name": "Chapter VI"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 6: Making decisions"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Program 6.1, Page number: 66"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#6.1.py\n",
+ "#Calculating the Absolute Value of an Integer\n",
+ "\n",
+ "#Variable Declaration/User input\n",
+ "number=12 #number=int(raw_input(\"Type in your number: \"))\n",
+ "\n",
+ "#Calculation\n",
+ "try:\n",
+ " if( number < 0 ):\n",
+ " number=-number #change sign,if number is negative\n",
+ "except: \n",
+ " print \"not a number\" #Invalid input/value error\n",
+ " \n",
+ "#Result\n",
+ "print (\"The absolute Value is {0}\".format(number))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The absolute Value is 12\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Program 6.2, Page number: 67"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#6.2.py\n",
+ "#Calculating average & counting the number\n",
+ "#of failures from a set of grades\n",
+ "\n",
+ "#Variable Declarations\n",
+ "gradeTotal=0\n",
+ "failureCount=0\n",
+ "i=0\n",
+ "\n",
+ "#User Input\n",
+ "numberOfGrades=5 #numberOfGrades=int(raw_input(\"How many grades will you be entering? \"))\n",
+ "grade=[72,83,91,89,95]\n",
+ "\n",
+ "#Calculation\n",
+ "while(i<numberOfGrades):\n",
+ " gradeTotal=gradeTotal+grade[i]\n",
+ " if(grade<65):\n",
+ " failureCount=failureCount+1;\n",
+ " i=i+1\n",
+ "\n",
+ "average=float(gradeTotal/numberOfGrades)\n",
+ " \n",
+ "#Result\n",
+ "print(\"Grade average= {0:.2f}\".format(average))\n",
+ "print(\"Number of failures= {0}\".format(failureCount))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Grade average= 86.00\n",
+ "Number of failures= 0\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Program 6.3, Page number: 70"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#6.3.py\n",
+ "#Determining if a Number Is Even or Odd\n",
+ "\n",
+ "#Variable declaration/User input\n",
+ "number_to_test=30 #number_to_test=int(raw_input(\"Enter your number to be tested: \"))\n",
+ "\n",
+ "#Calculation\n",
+ "remainder=number_to_test%2\n",
+ "\n",
+ "#Result\n",
+ "if(remainder==0):\n",
+ " print(\"The number is even\")\n",
+ "if(remainder!=0):\n",
+ " print(\"The number is odd\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The number is even\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Program 6.4, Page number: 71"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#6.4.py\n",
+ "#Program to determine if a number is even or odd (Ver. 2)\n",
+ "\n",
+ "#Variable declaration/User input\n",
+ "number_to_test=45 #number_to_test=int(raw_input(\"Enter your Number to be tested: \"))\n",
+ "\n",
+ "#Calculation\n",
+ "remainder=number_to_test%2\n",
+ "\n",
+ "#Result\n",
+ "if(remainder==0):\n",
+ " print(\"The number is even.\\n\")\n",
+ "else: #using 'else' instead of second 'if'\n",
+ " print(\"The number is odd.\\n\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The number is odd.\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Program 6.5, Page number: 73"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#6.5.py\n",
+ "#Determining if a Year Is a Leap Year\n",
+ "\n",
+ "#Variable declaration/User input\n",
+ "year=1900 #year=int(raw_input(\"Enter the year to be tested: \"))\n",
+ "\n",
+ "#Calculations\n",
+ "rem_4=year%4\n",
+ "rem_100=year%100\n",
+ "rem_400=year%400\n",
+ "\n",
+ "#Result\n",
+ "if((rem_4==0 and rem_100!=0)or rem_400==0):\n",
+ " print(\"It's a leap year.\")\n",
+ "else:\n",
+ " print(\"Nope, it's not a leap year.\\n\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Nope, it's not a leap year.\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Program 6.6, Page number: 77"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#6.6.py\n",
+ "#Implementing the Sign Function\n",
+ "\n",
+ "#Variable declaration/User input\n",
+ "number=450 #number=int(raw_input(\"Please type in a number: \"))\n",
+ "\n",
+ "#Calculations\n",
+ "try:\n",
+ " if(number<0): #Negative Number\n",
+ " sign=-1\n",
+ " elif(number==0): #No sign\n",
+ " sign=0\n",
+ " else: #Positive Number\n",
+ " sign=1\n",
+ "except: #Value error\n",
+ " print(\"invalid input\")\n",
+ "\n",
+ "#Result\n",
+ "print(\"Sign= {0}\".format(sign))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sign= 1\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Program 6.7, Page number: 78"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#6.7.py\n",
+ "#Implementing the Sign Function\n",
+ "\n",
+ "#variable declaration/User input\n",
+ "ch='z' #ch=raw_input(\"Enter a single character: \")\n",
+ "\n",
+ "#Calculation/Result\n",
+ "if((ch>='a' and ch <='z') or (ch>='A' and ch<='Z')):\n",
+ " print(\"It's an alphabetic character.\")\n",
+ "elif(ch>='0'and ch<='9'):\n",
+ " print(\"It's a digit.\")\n",
+ "else:\n",
+ " print(\"It's a special character\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "It's an alphabetic character.\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Program 6.8, Page number: 80"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#6.8.py\n",
+ "#evaluate simple expressions of the form\n",
+ "#<number operator number>\n",
+ "\n",
+ "\n",
+ "try:\n",
+ "#Variable declaration/User input\n",
+ " print(\"Type in your expression(with spaces inbetween): \")\n",
+ " value1, operator, value2=\"5 + 2\".split()\n",
+ " #value1, operator, value2=raw_input().split()\n",
+ "\n",
+ "except:\n",
+ " print(\"err.. follow the syntax <value operator value>\") \n",
+ " print(\"with spaces inbetween\\n\")\n",
+ "\n",
+ "#Parsing\n",
+ "value1,value2=[float(value1),float(value2)]\n",
+ "\n",
+ "#Calculation/Result\n",
+ "if(operator=='+'):\n",
+ " print(\"answer: {0:.2f}\".format(value1+value2))\n",
+ "elif(operator=='-'):\n",
+ " print(\"answer: {0:.2f}\".format(value1-value2))\n",
+ "elif(operator=='*'):\n",
+ " print(\"answer: {0:.2f}\".format(value1*value2))\n",
+ "else:\n",
+ " print(\"answer: {0:.2f}\".format(value1/value2))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Type in your expression(with spaces inbetween): \n",
+ "answer: 7.00\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Program 6.9, Page number: 85"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#6.9.py\n",
+ "#evaluate simple expressions of the form\n",
+ "#<number operator number> (Version 2)\n",
+ "\n",
+ "\n",
+ "try:\n",
+ "#Variable declaration/User input\n",
+ " print(\"Type in your expression(with spaces inbetween): \")\n",
+ " value1, operator, value2=\"3 * 5\".split()\n",
+ " #value1, operator, value2=raw_input().split()\n",
+ " \n",
+ " \n",
+ "except:\n",
+ " print(\"err.. follow the syntax <value operator value>\") \n",
+ " print(\"with spaces inbetween\\n\")\n",
+ "\n",
+ "#parsing\n",
+ "value1,value2=[float(value1), float(value2)]\n",
+ "\n",
+ "#Calculation/Result\n",
+ "if(operator=='+'):\n",
+ " print(\"Answer= {0:.2f}\".format(value1+value2))\n",
+ "elif(operator=='-'):\n",
+ " print(\"Answer= {0:.2f}\".format(value1-value2))\n",
+ "elif(operator=='*'):\n",
+ " print(\"Answer= {0:.2f}\".format(value1*value2))\n",
+ "elif(operator=='/'):\n",
+ " if(value2==0):\n",
+ " print(\"Whoops! divide by 0 issue\")\n",
+ " else:\n",
+ " print(\"Answer= {0:.2f}\".format(value1/value2))\n",
+ "else:\n",
+ " print(\"err.. Invalid operator\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Type in your expression(with spaces inbetween): \n",
+ "Answer= 15.00\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Program 6.10, Page number: 87"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#6.10.py\n",
+ "#Generating a Table of Prime Numbers\n",
+ "\n",
+ "#Variable declarations\n",
+ "p=2\n",
+ "\n",
+ "#Calculations\n",
+ "while(p<=50): #Outer loop\n",
+ " isPrime=1 #Variable declaration\n",
+ " d=2 \n",
+ " while(d<p): #Inner loop\n",
+ " if(p%d==0):\n",
+ " isPrime=0\n",
+ " d=d+1 #End of inner loop\n",
+ " \n",
+ " if( isPrime!=0):\n",
+ "#Print Result\n",
+ " print \" \",p \n",
+ " p=p+1 #End of outer loop"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " 2\n",
+ " 3\n",
+ " 5\n",
+ " 7\n",
+ " 11\n",
+ " 13\n",
+ " 17\n",
+ " 19\n",
+ " 23\n",
+ " 29\n",
+ " 31\n",
+ " 37\n",
+ " 41\n",
+ " 43\n",
+ " 47\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file