From 36a03d6d76bac315dba73b2ba9555c7e3fe0234f Mon Sep 17 00:00:00 2001 From: nice Date: Thu, 9 Oct 2014 18:07:00 +0530 Subject: updated books --- .../KamthaneChapter4.ipynb | 972 +++++++++++++++++++++ 1 file changed, 972 insertions(+) create mode 100755 Programming_in_C_using_ANSI_C/KamthaneChapter4.ipynb (limited to 'Programming_in_C_using_ANSI_C/KamthaneChapter4.ipynb') diff --git a/Programming_in_C_using_ANSI_C/KamthaneChapter4.ipynb b/Programming_in_C_using_ANSI_C/KamthaneChapter4.ipynb new file mode 100755 index 00000000..758457a3 --- /dev/null +++ b/Programming_in_C_using_ANSI_C/KamthaneChapter4.ipynb @@ -0,0 +1,972 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Chapter 4: Input & Output in C

" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Example 4.1, Page number: 47

" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#show the effect of mismatch of data types\n", + "\n", + "#Reads Input value for a\n", + "\n", + "a = raw_input(\"Enter value of 'A' : \")\n", + "a = int(a)\n", + "\n", + "#In python, raw_input() is used to read values\n", + "\n", + "#Result\n", + "print \"A = \",a\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter value of 'A' : 8\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "A = 8\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Example 4.2, Page number: 47

" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#show the effect of mismatch of data types\n", + "\n", + "import sys\n", + "\n", + "#Variable initialization\n", + "#Reads Input value for a\n", + "\n", + "a = raw_input(\"Enter value of 'A' : \")\n", + "a = int(a)\n", + "\n", + "#In python, raw_input() is used to read values\n", + "\n", + "#there is no char datatype of 1 byte size in python.\n", + "#python has string variable only.\n", + "\n", + "#Result\n", + "\n", + "sys.stdout.write(\"A = %d\"%(a%256))\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter value of 'A' : 256\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "A = 0" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Example 4.3, Page number: 49

" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#show the effect of various escape sequences\n", + "\n", + "import sys\n", + "\n", + "#Variable initialization\n", + "a = 1\n", + "b = a + 1\n", + "c = b + 1\n", + "d = c + 1\n", + "\n", + "#Result\n", + "sys.stdout.write ('\\tA = %d\\nB = %d \\'C = %d\\''%(a,b,c))\n", + "sys.stdout.write ('\\n**D = %d**'%(d))\n", + "sys.stdout.write ('\\nA = %d B = %d'%(a,b))\n", + "sys.stdout.write ('\\r******')\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\tA = 1\n", + "B = 2 'C = 3'\n", + "**D = 4**\n", + "A = 1 B = 2******" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Example 4.4, Page number: 50

" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#print the third power of 2 using pow() function.\n", + "#assume the floating point numbers\n", + "\n", + "#Variable initialization\n", + "x = 2.0\n", + "y = 3.0\n", + "\n", + "#Result\n", + "print '%lf raised to %lf is %lf\\n' %(x,y,pow(x,y))\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "2.000000 raised to 3.000000 is 8.000000\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Example 4.5, Page number: 50

" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#print the third power of 10 using pow() function.\n", + "#assume the floating point numbers\n", + "\n", + "#Variable initialization\n", + "p = 3\n", + "\n", + "#Result\n", + "#There is no pow10() function in Python.\n", + "print 'Ten raised to %lf is %lf\\n' %(p,pow(10,p))\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Ten raised to 3.000000 is 1000.000000\n", + "\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Example 4.6, Page number: 51

" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#detect an error while inputting a data. Use return value of scanf() statement.\n", + "\n", + "#Variable initialization\n", + "#in python,the map(int,a.split()) function splits the input characters\n", + "#and converts it into integers and returns a value if all the values are integers\n", + "#otherwise an exception will be generated and initializes v as 0.\n", + "#using exception handling, this result can be achieved.\n", + "\n", + "\n", + "#Exception handling\n", + "try:\n", + " a = raw_input(\"Enter value of 'A','B' & 'C' : \")\n", + " v = map(int,a.split())\n", + "except:\n", + " v = 0\n", + "\n", + "#In python, raw_input() is used to read values\n", + "\n", + "#Result\n", + "print '\\nError In Inputting.' if v < 3 else '\\nValues Successfully read.'\n", + "\n", + " \n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter value of 'A','B' & 'C' : 1 2 3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Values Successfully read.\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Example 4.7, Page number: 51

" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Find length of the string using print() function\n", + "\n", + "import sys\n", + "\n", + "#Variable initialization\n", + "#in python, print()or write() function would not return the number of characters\n", + "#written to the output buffer. so len() function is used to find the length.\n", + "\n", + "nm = raw_input(\"Enter String : \")\n", + "\n", + "#In python, raw_input() is used to read values\n", + "\n", + "l = len(nm)\n", + "\n", + "#Result\n", + "print '\\nLength = ',l\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter String : HELLO\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Length = 5\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Example 4.8, Page number: 52

" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Perform addition of two numbers\n", + "\n", + "#Variable initialization\n", + "a = raw_input(\"ENTER TWO VALUES \")\n", + "b = raw_input(\"ENTER TWO VALUES \")\n", + "\n", + "#In python, raw_input() is used to read values\n", + "\n", + "c = int(a) + int(b)\n", + "\n", + "#Result\n", + "print '\\nSUM IS = ',c\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "ENTER TWO VALUES 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "ENTER TWO VALUES 8\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "SUM IS = 13\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Example 4.9, Page number: 52

" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Find the square of the given number\n", + "\n", + "#Variable initialization\n", + "a = raw_input(\"ENTER ANY NUMBER \")\n", + "\n", + "#In python, raw_input() is used to read values\n", + "\n", + "c = int(a) * int(a)\n", + "\n", + "#Result\n", + "print '\\nSQUARE OF GIVEN NUMBER = ',c\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "ENTER ANY NUMBER 5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "SQUARE OF GIVEN NUMBER = 25\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Example 4.10, Page number: 53

" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Calculate average of three real numbers.\n", + "\n", + "#Variable initialization\n", + "\n", + "a = raw_input(\"ENTER THREE FLOAT NUMBERS : \")\n", + "b = raw_input(\"ENTER THREE FLOAT NUMBERS : \")\n", + "c = raw_input(\"ENTER THREE FLOAT NUMBERS : \")\n", + "\n", + "#In python, raw_input() is used to read values\n", + "\n", + "d = float(a) + float(b) + float(c)\n", + "\n", + "#Result\n", + "print '\\nAverage of Given Numbers : ',d/3\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "ENTER THREE FLOAT NUMBERS : 2.5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "ENTER THREE FLOAT NUMBERS : 3.5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "ENTER THREE FLOAT NUMBERS : 4.5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Average of Given Numbers : 3.5\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Example 4.11, Page number: 53

" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Print message with inputted name\n", + "\n", + "#Variable initialization\n", + "yourname = raw_input(\"Enter Your Name : \")\n", + "\n", + "#In python, raw_input() is used to read values\n", + "\n", + "#Result\n", + "print \"\\nWel Come %s to 'C' Programming Course\"%(yourname)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Your Name : Ajay\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Wel Come Ajay to 'C' Programming Course\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Example 4.12, Page number: 54

" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Input a single character and display it\n", + "\n", + "#Variable initialization\n", + "ch = raw_input(\"Enter any character : \")\n", + "\n", + "#In python, raw_input() is used to read values\n", + "\n", + "#Result\n", + "print \"\\nYour Entered Character is : %c\"%(ch)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any character : C\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Your Entered Character is : C\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Example 4.13, Page number: 54

" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Swap the values of two variables without using third variable.\n", + "\n", + "#Variable initialization\n", + "a = 7\n", + "b = 4\n", + "\n", + "print \"\\n A = %d B = %d\"%(a,b)\n", + "\n", + "#Swapping\n", + "a = a + b\n", + "b = a - b\n", + "a = a - b\n", + "\n", + "#Result\n", + "print \"Now A = %d B = %d\"%(a,b)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + " A = 7 B = 4\n", + "Now A = 4 B = 7\n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Example 4.14, Page number: 55

" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Accept characters through keyboard using getchar() function\n", + "\n", + "import sys\n", + "\n", + "#Variable initialization\n", + "#in python, string is an object. It has no nul-termination character.\n", + "#raw_input() reads the string constant from the stdin and the write() function\n", + "#prints it to the stdout based on the attribute length.\n", + "\n", + "ch = raw_input(\" \")\n", + "\n", + "#In python, raw_input() is used to read values\n", + "\n", + "#Result\n", + "sys.stdout.write(ch)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + " COMPILER\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "COMPILER" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Example 4.15, Page number: 56

" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Print the characters using putchar() function\n", + "\n", + "import sys\n", + "\n", + "#Variable initialization\n", + "#in python, string is an object. It has no nul-termination character.\n", + "#raw_input() reads the string constant from the stdin.\n", + "\n", + "ch = raw_input(\"Enter Text Here : \")\n", + "\n", + "#In python, raw_input() is used to read values\n", + "\n", + "#Result\n", + "#using for loop, the string can be displayed character by character.\n", + "\n", + "sys.stdout.write(\"The Entered Text : \")\n", + "for c in ch:\n", + " sys.stdout.write(c)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Text Here : Characters\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The Entered Text : Characters" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Example 4.16, Page number: 56

" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Show the effect of getche() and getch() functions\n", + "\n", + "import sys\n", + "\n", + "#Variable initialization\n", + "#in python, there is no equivalent function for getche() and getch() functions.\n", + "#raw_input() function reads the characters and terminates by pressing the\n", + "#enter key\n", + "\n", + "ch = raw_input(\"Enter any two alphabetics \")\n", + "\n", + "#In python, raw_input() is used to read values\n", + "#Result\n", + "sys.stdout.write(ch)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any two alphabetics A \n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "A " + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Example 4.17, Page number: 57

" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Read and display the character using getch() and putch() functions\n", + "\n", + "import sys\n", + "\n", + "#Variable initialization\n", + "#in python, there is no equivalent function for getch() function.\n", + "#raw_input() function reads the characters and terminates by pressing the\n", + "#enter key\n", + "\n", + "ch = raw_input(\"Press any key to continue \")\n", + "\n", + "#In python, raw_input() is used to read values\n", + "\n", + "#Result\n", + "sys.stdout.write(\"You Pressed : %s\"%(ch))\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Press any key to continue 9\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "You Pressed : 9" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Example 4.18, Page number: 57

" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Accept string through the keyboard using gets() function\n", + "\n", + "import sys\n", + "\n", + "#Variable initialization\n", + "#in python, raw_input() function reads the string and terminates by pressing the\n", + "#enter key and write() function displays the string.\n", + "\n", + "ch = raw_input(\"Enter the String : \")\n", + "\n", + "#In python, raw_input() is used to read values\n", + "\n", + "#Result\n", + "sys.stdout.write(\"Entered String : %s\"%(ch))\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the String : USE OF GETS()\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Entered String : USE OF GETS()" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Example 4.19, Page number: 58

" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Print the accepted character using puts() function\n", + "\n", + "import sys\n", + "\n", + "#Variable initialization\n", + "#in python, raw_input() function reads the string and terminates by pressing the\n", + "#enter key and print() function displays the string.\n", + "\n", + "ch = raw_input(\"Enter the String : \")\n", + "\n", + "#In python, raw_input() is used to read values\n", + "\n", + "#Result\n", + "print\"Entered String : \"\n", + "print ch\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the String : puts is in use.\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Entered String : \n", + "puts is in use.\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Example 4.20, Page number: 58

" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#read string using cgets() and display it using cputs()\n", + "\n", + "import sys\n", + "\n", + "#Variable initialization\n", + "#in python, raw_input() function reads the string and terminates by pressing the\n", + "#enter key and print() function displays the string.\n", + "#in python, string is an object.\n", + "\n", + "t = raw_input(\"Enter Text Here : \")\n", + "\n", + "#In python, raw_input() is used to read values\n", + "\n", + "#Result\n", + "print \"Your Entered Text : %s\"%(t)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Text Here : How are you?\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Your Entered Text : How are you?\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file -- cgit