diff options
author | Thomas Stephen Lee | 2015-09-04 22:04:10 +0530 |
---|---|---|
committer | Thomas Stephen Lee | 2015-09-04 22:04:10 +0530 |
commit | 41f1f72e9502f5c3de6ca16b303803dfcf1df594 (patch) | |
tree | f4bf726a3e3ce5d7d9ee3781cbacfe3116115a2c /Magnifying_C_by_Arpita_Goyal/Chapter_3_3.ipynb | |
parent | 9c9779ba21b9bedde88e1e8216f9e3b4f8650b0e (diff) | |
download | Python-Textbook-Companions-41f1f72e9502f5c3de6ca16b303803dfcf1df594.tar.gz Python-Textbook-Companions-41f1f72e9502f5c3de6ca16b303803dfcf1df594.tar.bz2 Python-Textbook-Companions-41f1f72e9502f5c3de6ca16b303803dfcf1df594.zip |
add/remove/update books
Diffstat (limited to 'Magnifying_C_by_Arpita_Goyal/Chapter_3_3.ipynb')
-rwxr-xr-x | Magnifying_C_by_Arpita_Goyal/Chapter_3_3.ipynb | 1060 |
1 files changed, 1060 insertions, 0 deletions
diff --git a/Magnifying_C_by_Arpita_Goyal/Chapter_3_3.ipynb b/Magnifying_C_by_Arpita_Goyal/Chapter_3_3.ipynb new file mode 100755 index 00000000..9e566fc1 --- /dev/null +++ b/Magnifying_C_by_Arpita_Goyal/Chapter_3_3.ipynb @@ -0,0 +1,1060 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 3: Simple Programs" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 1, Page number: 67" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Calculation and result\n", + "pi = 3.141\n", + "num = int(raw_input('Enter your favorite number: '))\n", + "print ('You have entered your favorite number as %d' % num)\n", + "c = raw_input('Enter your favorite character: ')\n", + "print ('You have entered your favorite character as %c' % c)\n", + "print ('The value of pi is %.3f ' % pi)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your favorite number: 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "You have entered your favorite number as 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your favorite character: m\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "You have entered your favorite character as m\n", + "The value of pi is 3.141 \n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.1, Page number: 70" + ] + }, + { + "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\n", + "som = a+b\n", + "\n", + "# Result\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 67\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter second number 990\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The sum of 67 + 990 = 1057 \n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.2, Page number: 71" + ] + }, + { + "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", + "if a>b :\n", + " print ('The greater number is %d ' % a)\n", + "else :\n", + " print ('The greater number is %d ' % b)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter first number 78\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter second number 190\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The greater number is 190 \n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.3, Page number: 72" + ] + }, + { + "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", + "if a==b :\n", + " print ('Both numbers are equal ')\n", + "else :\n", + " if a>b :\n", + " print ('The greater number is %d ' % a)\n", + " else :\n", + " print ('The greater number is %d ' % b)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter first number 78\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter second number 78\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Both numbers are equal \n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.4, Page number: 73" + ] + }, + { + "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", + "# Calculation and result\n", + "if a>b :\n", + " if a>c :\n", + " print ('The greatest number is %d ' % a)\n", + " else :\n", + " print ('The greatest number is %d ' % c)\n", + "\n", + "else :\n", + " if b>c :\n", + " print ('The greatest number is %d ' % b)\n", + " else : \n", + " print ('The greatest number is %d ' % c)" + ], + "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 128\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter third number 90\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The greatest number is 128 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.5, Page number: 74" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Variable declaration\n", + "n = int(raw_input('Enter any number '))\n", + "\n", + "# Calculation and result\n", + "print ('Counting upto %d ' % n)\n", + "\n", + "for i in range (1, n+1) :\n", + " print ('%d ' % i)," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any number 7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Counting upto 7 \n", + "1 2 3 4 5 6 7 \n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.6, Page number: 75" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Variable declaration\n", + "n = int(raw_input('Enter any number '))\n", + "\n", + "# Calculation and result\n", + "print ('Even numbers upto %d ' % n)\n", + "\n", + "for i in range (2, n+1, 2) :\n", + " print ('%d ' % i)," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any number 10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Even numbers upto 10 \n", + "2 4 6 8 10 \n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.7, Page number: 75" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Variable declaration\n", + "n = int(raw_input('Enter any number '))\n", + "\n", + "# Calculation and result\n", + "print ('Odd numbers upto %d ' % n)\n", + "\n", + "for i in range (1, n+1, 2) :\n", + " print ('%d ' % i)," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any number 10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Odd numbers upto 10 \n", + "1 3 5 7 9 \n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.8, Page number: 76" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Variable declaration\n", + "n = int(raw_input('Enter any number '))\n", + "\n", + "# Calculation and result\n", + "print ('Number line upto %d ' % n)\n", + "\n", + "for i in range (-n, n+1) :\n", + " print ('%d ' % i)," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any number 7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Number line upto 7 \n", + "-7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 \n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.9, Page number: 77" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Variable declaration\n", + "n = int(raw_input('Enter any number '))\n", + "sum = 0\n", + "\n", + "# Calculation\n", + "for i in range (1, n+1) :\n", + " sum = sum + i\n", + "\n", + "# Result\n", + "print ('Sum of %d natural numbers is %d ' % (n, sum))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any number 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sum of 6 natural numbers is 21 \n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.10, Page number: 78" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Variable declaration\n", + "n = int(raw_input('Enter any number '))\n", + "\n", + "# Calculation and result\n", + "print ('The factors of %d are ' % n)\n", + "\n", + "for i in range (1, n+1) :\n", + " if n%i == 0 :\n", + " print ('%d ' % i)," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any number 28\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The factors of 28 are \n", + "1 2 4 7 14 28 \n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.11, Page number: 79" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Variable declaration\n", + "n = int(raw_input('Enter any number '))\n", + "\n", + "# Calculation and result\n", + "print ('The first 10 multiples of %d are ' % n)\n", + "\n", + "for i in range (1, 11) :\n", + " print ('%d X %d = %d ' % (n, i, n*i))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any number 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The first 10 multiples of 6 are \n", + "6 X 1 = 6 \n", + "6 X 2 = 12 \n", + "6 X 3 = 18 \n", + "6 X 4 = 24 \n", + "6 X 5 = 30 \n", + "6 X 6 = 36 \n", + "6 X 7 = 42 \n", + "6 X 8 = 48 \n", + "6 X 9 = 54 \n", + "6 X 10 = 60 \n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.12, Page number: 80" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Variable declaration\n", + "n = int(raw_input('Enter any number '))\n", + "i = 1\n", + "sum = 0\n", + "\n", + "# Calculation\n", + "print ('The sum of digits of %d is' % n),\n", + "\n", + "while n > 0 :\n", + " sum = sum + n%10\n", + " n = n/10\n", + "\n", + "# Result\n", + "print ('%d' % sum)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any number 5214\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The sum of digits of 5214 is 12\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.13, Page number: 81" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Variable declaration\n", + "n = int(raw_input('Enter any number '))\n", + "flag = 1\n", + "\n", + "# Calculation\n", + "for i in range (2, n) :\n", + " if flag == 1 :\n", + " if n%i == 0 :\n", + " flag = 0\n", + "\n", + "# Result\n", + "if flag :\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": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.14, Page number: 82" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Variable declaration\n", + "n = int(raw_input('Enter any number '))\n", + "first = 0\n", + "second = 1\n", + "count = 3\n", + "\n", + "# Calculation and result\n", + "print ('%d Fibonacci terms are ' % n)\n", + "print ('%d %d ' % (first, second)),\n", + "\n", + "while count<=n :\n", + " third = first + second\n", + " first = second\n", + " second = third\n", + " print ('%d ' % third),\n", + " count = count + 1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any number 10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "10 Fibonacci terms are \n", + "0 1 1 2 3 5 8 13 21 34 \n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.15, Page number: 83" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Variable declaration\n", + "n = int(raw_input('Enter any number '))\n", + "sum = n\n", + "\n", + "# Calculation\n", + "print ('Sum of digits of %d is' % n),\n", + "\n", + "while sum>=10 :\n", + " sum = 0\n", + " while n>0 :\n", + " sum = sum + n%10\n", + " n = n/10\n", + " n = sum\n", + "\n", + "# Result\n", + "print ('%d' % sum)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any number 8626\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sum of digits of 8626 is 4\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.16, Page number: 84" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Variable declaration\n", + "n = int(raw_input('Enter any number '))\n", + "\n", + "# Calculation and result\n", + "print ('Prime numbers upto %d are ' % n)\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)," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any number 12\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Prime numbers upto 12 are \n", + "2 3 5 7 11 \n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.17, Page number: 85" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Variable declaration\n", + "n = int(raw_input('Enter a number '))\n", + "\n", + "# Calculation and result\n", + "for lin in range (1, n+1) :\n", + " for count in range (1, lin+1) :\n", + " print ('%d ' % count),\n", + " print" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a number 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1 \n", + "1 2 \n", + "1 2 3 \n", + "1 2 3 4 \n", + "1 2 3 4 5 \n", + "1 2 3 4 5 6 \n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.18, Page number: 86" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Variable declaration\n", + "n = int(raw_input('Enter a number '))\n", + "\n", + "# Calculation and result\n", + "for lin in range (1, n+1) :\n", + " for count in range (lin, 2*lin) :\n", + " print ('%d ' % count),\n", + " print" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a number 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1 \n", + "2 3 \n", + "3 4 5 \n", + "4 5 6 7 \n", + "5 6 7 8 9 \n", + "6 7 8 9 10 11 \n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.19, Page number: 87" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Variable declaration\n", + "n = int(raw_input('Enter a number '))\n", + "\n", + "# Calculation and result\n", + "for lin in range (1, n+1) :\n", + " for blk in range (1, n-lin+1) :\n", + " print ' ',\n", + " for up in range (1, lin+1) :\n", + " print ('%d' % up),\n", + " down = lin-1\n", + " while down>0 :\n", + " print ('%d' % down),\n", + " down = down-1\n", + " print" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a number 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " 1\n", + " 1 2 1\n", + " 1 2 3 2 1\n", + " 1 2 3 4 3 2 1\n", + " 1 2 3 4 5 4 3 2 1\n", + "1 2 3 4 5 6 5 4 3 2 1\n" + ] + } + ], + "prompt_number": 19 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 3.20, Page number: 88" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Variable declaration\n", + "n = int(raw_input('Enter a number '))\n", + "\n", + "# Calculation and result\n", + "for lin in range (1, n+1) :\n", + " for blk in range (1, n-lin+1) :\n", + " print ' ',\n", + " for up in range (lin, 2*lin) :\n", + " print ('%d' % (up%10)),\n", + " down = 2*lin-2\n", + " while down>=lin :\n", + " print ('%d' % (down%10)),\n", + " down = down-1\n", + " print" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a number 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " 1\n", + " 2 3 2\n", + " 3 4 5 4 3\n", + " 4 5 6 7 6 5 4\n", + " 5 6 7 8 9 8 7 6 5\n", + "6 7 8 9 0 1 0 9 8 7 6\n" + ] + } + ], + "prompt_number": 20 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |