summaryrefslogtreecommitdiff
path: root/Magnifying_C/Chapter_5.ipynb
diff options
context:
space:
mode:
authorhardythe12015-04-07 15:58:05 +0530
committerhardythe12015-04-07 15:58:05 +0530
commit92cca121f959c6616e3da431c1e2d23c4fa5e886 (patch)
tree205e68d0ce598ac5caca7de839a2934d746cce86 /Magnifying_C/Chapter_5.ipynb
parentb14c13fcc6bb6d01c468805d612acb353ec168ac (diff)
downloadPython-Textbook-Companions-92cca121f959c6616e3da431c1e2d23c4fa5e886.tar.gz
Python-Textbook-Companions-92cca121f959c6616e3da431c1e2d23c4fa5e886.tar.bz2
Python-Textbook-Companions-92cca121f959c6616e3da431c1e2d23c4fa5e886.zip
added books
Diffstat (limited to 'Magnifying_C/Chapter_5.ipynb')
-rwxr-xr-xMagnifying_C/Chapter_5.ipynb909
1 files changed, 909 insertions, 0 deletions
diff --git a/Magnifying_C/Chapter_5.ipynb b/Magnifying_C/Chapter_5.ipynb
new file mode 100755
index 00000000..b4cb2db2
--- /dev/null
+++ b/Magnifying_C/Chapter_5.ipynb
@@ -0,0 +1,909 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 5: Pointers"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.1, Page number: 135"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number: '))\n",
+ "b = int(raw_input('Enter second number: '))\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('The original values are a = %d b = %d' % (a, b))\n",
+ "temp = a\n",
+ "a = b\n",
+ "b = temp\n",
+ "\n",
+ "print ('The values after swapping are a = %d b = %d' % (a, b))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number: 15\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number: 27\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The original values are a = 15 b = 27\n",
+ "The values after swapping are a = 27 b = 15\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.2, Page number: 136"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number: '))\n",
+ "b = int(raw_input('Enter second number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "print ('The original values are a = %d b = %d' % (a, b))\n",
+ "\n",
+ "def swap (a, b) :\n",
+ " temp = a\n",
+ " a = b\n",
+ " b = temp\n",
+ " print ('The values after swapping are a = %d b = %d' % (a, b))\n",
+ "\n",
+ "swap (a, b)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number: 15\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number: 27\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The original values are a = 15 b = 27\n",
+ "The values after swapping are a = 27 b = 15\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.3, Page number: 141"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number: '))\n",
+ "b = int(raw_input('Enter second number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "print ('The original values are a = %d b = %d' % (a, b))\n",
+ "\n",
+ "def swap (a, b) :\n",
+ " temp = a\n",
+ " a = b\n",
+ " b = temp\n",
+ " print ('The values after swapping are a = %d b = %d' % (a, b))\n",
+ "\n",
+ "swap (a, b)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number: 15\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number: 27\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The original values are a = 15 b = 27\n",
+ "The values after swapping are a = 27 b = 15\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.4, Page number: 145"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number: '))\n",
+ "b = int(raw_input('Enter second number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "print ('In main the original values are a = %d b = %d' % (a, b))\n",
+ "print ('In main the address of a is %x and address of b is %x' % (a, b))\n",
+ "\n",
+ "def swap (a, b) :\n",
+ " print ('In swap the address in a is %x and b is %x' % (a, b))\n",
+ " temp = a\n",
+ " a = b\n",
+ " b = temp\n",
+ " print ('The values after swapping are a = %d b = %d' % (a, b))\n",
+ "\n",
+ "swap (a, b)\n",
+ "\n",
+ "print ('In main the values after swapping are a = %d b = %d' % (a, b))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number: 15\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number: 27\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "In main the original values are a = 15 b = 27\n",
+ "In main the address of a is f and address of b is 1b\n",
+ "In swap the address in a is f and b is 1b\n",
+ "The values after swapping are a = 27 b = 15\n",
+ "In main the values after swapping are a = 15 b = 27\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.5, Page number: 148"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number: '))\n",
+ "b = int(raw_input('Enter second number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "print ('In main the value of a = %d b = %d' % (a, b))\n",
+ "\n",
+ "def doooo (a, b) :\n",
+ " a = a+10\n",
+ " b = b+10\n",
+ "\n",
+ "doooo (a, b)\n",
+ "\n",
+ "print ('In main the value of a = %d b = %d' % (a, b))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number: 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number: 7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "In main the value of a = 5 b = 7\n",
+ "In main the value of a = 5 b = 7\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.6, Page number: 150"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number: '))\n",
+ "b = int(raw_input('Enter second number: '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "print ('In main the value of a = %d b = %d' % (a, b))\n",
+ "print ('In main the addresses are a = %s b = %s' % (hex(id(a)), hex(id(b))))\n",
+ "\n",
+ "def doooo (a, b) :\n",
+ " print ('In doooo the addresses are a = %s b = %s' % (hex(id(a)), hex(id(b))))\n",
+ " a = a+10\n",
+ " b = b+10\n",
+ "\n",
+ "doooo (a, b)\n",
+ "\n",
+ "print ('In main the value of a = %d b = %d' % (a, b))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number: 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number: 7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "In main the value of a = 5 b = 7\n",
+ "In main the addresses are a = 0x1b49138 b = 0x1b49108\n",
+ "In doooo the addresses are a = 0x1b49138 b = 0x1b49108\n",
+ "In main the value of a = 5 b = 7\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.7, Page number: 152"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number '))\n",
+ "b = int(raw_input('Enter second number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def add (a, b) :\n",
+ " x = a+b\n",
+ " return x\n",
+ "\n",
+ "som = add (a, b)\n",
+ "\n",
+ "print ('The sum of %d + %d = %d' % (a, b, som))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number 2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The sum of 2 + 3 = 5\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.8, Page number: 154"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number '))\n",
+ "b = int(raw_input('Enter second number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def greater (a, b) :\n",
+ " if a>b :\n",
+ " big = a\n",
+ " else :\n",
+ " big = b\n",
+ " return big\n",
+ "\n",
+ "great = greater (a, b)\n",
+ "\n",
+ "print ('The greater number is %d' % great)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number 45\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number 23\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The greater number is 45\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.9, Page number: 156"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number '))\n",
+ "b = int(raw_input('Enter second number '))\n",
+ "c = int(raw_input('Enter third number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def greater (a, b, c) :\n",
+ " if a>b :\n",
+ " if a>c :\n",
+ " big = a\n",
+ " else :\n",
+ " big = c\n",
+ " else :\n",
+ " if b>c :\n",
+ " big = b\n",
+ " else :\n",
+ " big = c\n",
+ " return big\n",
+ "\n",
+ "great = greater (a, b, c)\n",
+ "\n",
+ "print ('The greatest number is %d' % great)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number 45\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number 190\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter third number 128\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The greatest number is 190\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.10, Page number: 158"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def summate (n) :\n",
+ " i = 1\n",
+ " total = 0\n",
+ " while i <= n :\n",
+ " total = total + i\n",
+ " i = i+1\n",
+ " return total\n",
+ "\n",
+ "sumn = summate (n)\n",
+ "\n",
+ "print ('Sum of first %d natural numbers is %d' % (n, sumn))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sum of first 5 natural numbers is 15\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.11, Page number: 160"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def sumofdigit (n) :\n",
+ " sum = 0\n",
+ " while n > 0 :\n",
+ " sum = sum + n%10\n",
+ " n = n/10\n",
+ " return sum\n",
+ "\n",
+ "result = sumofdigit (n)\n",
+ "\n",
+ "print ('Sum of digits of %d is %d' % (n, result))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 7162\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sum of digits of 7162 is 16\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.12, Page number: 161"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "sum = n\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def sumofdigit (n, sum) :\n",
+ " while sum>=10 :\n",
+ " sum = 0\n",
+ " while n>0 :\n",
+ " sum = sum + n%10\n",
+ " n = n/10\n",
+ " n = sum\n",
+ " return sum\n",
+ "\n",
+ "result = sumofdigit (n, sum)\n",
+ "\n",
+ "print ('Sum of digits of %d is %d' % (n, result))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 7162\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sum of digits of 7162 is 7\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.13, Page number: 163"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def checkprime (n) :\n",
+ " flag = 1\n",
+ " i = 2\n",
+ " while i<n and flag :\n",
+ " if n%i == 0 :\n",
+ " flag = 0\n",
+ " i = i+1\n",
+ " return flag\n",
+ "\n",
+ "result = checkprime (n)\n",
+ "\n",
+ "if result :\n",
+ " print ('%d is Prime ' % n)\n",
+ "else :\n",
+ " print ('%d is Composite ' % n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 41\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "41 is Prime \n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.14, Page number: 165"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def checkprime (n) :\n",
+ " for num in range (2, n+1) :\n",
+ " flag = 1\n",
+ " for i in range (2, num) :\n",
+ " if num % i == 0 : \n",
+ " flag = 0\n",
+ " if flag :\n",
+ " print ('%d ' % num),\n",
+ "\n",
+ "checkprime (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 11\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2 3 5 7 11 \n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.15, Page number: 167"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def makethird (n) :\n",
+ " first = 0\n",
+ " second = 1\n",
+ " count = 3\n",
+ " print ('%d terms of Fibonacci series are ' % n)\n",
+ " print ('%d %d ' % (first, second)),\n",
+ " while count<=n :\n",
+ " third = first + second\n",
+ " first = second\n",
+ " second = third\n",
+ " print (' %d ' % third),\n",
+ " count = count + 1\n",
+ "\n",
+ "makethird (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10 terms of Fibonacci series are \n",
+ "0 1 1 2 3 5 8 13 21 34 \n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.16, Page number: 169"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "n = int(raw_input('Enter any number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def makethird (n) :\n",
+ " first = 0\n",
+ " second = 1\n",
+ " third = 0\n",
+ " print ('Fibonacci series up to %d is ' % n)\n",
+ " print first,\n",
+ " while third<=n :\n",
+ " print (' %d ' % second),\n",
+ " third = first + second\n",
+ " first = second\n",
+ " second = third\n",
+ "\n",
+ "makethird (n)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any number 100\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Fibonacci series up to 100 is \n",
+ "0 1 1 2 3 5 8 13 21 34 55 89 \n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file