summaryrefslogtreecommitdiff
path: root/The_Spirit_of_C/Chapter1.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'The_Spirit_of_C/Chapter1.ipynb')
-rwxr-xr-xThe_Spirit_of_C/Chapter1.ipynb198
1 files changed, 198 insertions, 0 deletions
diff --git a/The_Spirit_of_C/Chapter1.ipynb b/The_Spirit_of_C/Chapter1.ipynb
new file mode 100755
index 00000000..80b53e61
--- /dev/null
+++ b/The_Spirit_of_C/Chapter1.ipynb
@@ -0,0 +1,198 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:1c2e6d0b5736bd35dd626e4e22d0dbe67f82011189ddb91d07585ae3f73aa6b8"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 1, Elementary Programming"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Program 1-1 , Page number: 2"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# An interactive program to calculate the hypotenuse of a right triangle\n",
+ "\n",
+ "import math ## library for using sqrt function\n",
+ "\n",
+ "def square(a):\n",
+ "\t\"\"\"Returns the square of the given number \"\"\"\n",
+ "\treturn a*a\n",
+ "\n",
+ "print \"Enter the lengths of the two legs of a right triangle\"\n",
+ "print \"The program will display the length of the hypotenuse.\"\n",
+ "print \"Enter zero or negative values to end program\"\n",
+ "\n",
+ "# Priming reads follow\n",
+ "print \"First leg (0.0 to quit)? \",\n",
+ "a_leg = 3\n",
+ "print a_leg\n",
+ "print \"Second leg (0.0 to quit)? \",\n",
+ "b_leg = 4\n",
+ "print b_leg\n",
+ "\n",
+ "while a_leg>0.0 and b_leg>0.0:\n",
+ "\tprint \"The legs of the right triangle are %.2f and %.2f,\\n\"%(a_leg,b_leg),\n",
+ "\t# Hypotenuse is square root of a*a + b*b\n",
+ "\thypotenuse = math.sqrt(square(a_leg) + square(b_leg))\n",
+ "\tprint \" so the hypotenuse is %.2f\\n\\n\"%hypotenuse,\n",
+ "\tprint \"First leg (0.0 to quit)? \",\n",
+ "\ta_leg = 0.0\t\t\t# To terminate the loop\n",
+ "\tprint a_leg\n",
+ "\tprint \"Second leg (0.0 to quit)? \",\n",
+ "\tb_leg = 0.0\n",
+ "\tprint b_leg\n",
+ "\n",
+ "print \"Thank you.\"\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the lengths of the two legs of a right triangle\n",
+ "The program will display the length of the hypotenuse.\n",
+ "Enter zero or negative values to end program\n",
+ "First leg (0.0 to quit)? 3\n",
+ "Second leg (0.0 to quit)? 4\n",
+ "The legs of the right triangle are 3.00 and 4.00,\n",
+ " so the hypotenuse is 5.00\n",
+ "\n",
+ "First leg (0.0 to quit)? 0.0\n",
+ "Second leg (0.0 to quit)? 0.0\n",
+ "Thank you.\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Program 1-2 , Page number: 3"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def main(): \n",
+ "\t\"\"\"simply returns a string\"\"\"\n",
+ "\t# There's no main() function in python.This main() function is no different that any other user-defined functions\n",
+ "\treturn \"Welcome to C programming!\\n\"\n",
+ "\t\n",
+ "print main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Welcome to C programming!\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Program 1-3 , Page number: 6"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Use of braces \n",
+ "\n",
+ "def main():\n",
+ "\t# in python, proper indentation works as braces\n",
+ "\t# the following lines come under the main() function block due to indentation\n",
+ "\tprint \"Give me land, lots of land\",\n",
+ "\tprint \"And the starry skies above...\\n\",\n",
+ "\n",
+ "main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Give me land, lots of land And the starry skies above...\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Program 1-4 , Page number: 8"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# A short poem\n",
+ "\n",
+ "def main():\n",
+ "\tprint \"Mary had a little lamb,\",\n",
+ "\tprint \"its fleece was white as snow.\\n\",\n",
+ "\tprint \"And everywhere that Mary went,\",\n",
+ "\tprint \"the lamb was sure to go.\\n\"\n",
+ "\n",
+ "main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Mary had a little lamb, its fleece was white as snow.\n",
+ "And everywhere that Mary went, the lamb was sure to go.\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file