summaryrefslogtreecommitdiff
path: root/C++_Programming_In_Easy_Steps/Chapter2.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'C++_Programming_In_Easy_Steps/Chapter2.ipynb')
-rwxr-xr-xC++_Programming_In_Easy_Steps/Chapter2.ipynb419
1 files changed, 419 insertions, 0 deletions
diff --git a/C++_Programming_In_Easy_Steps/Chapter2.ipynb b/C++_Programming_In_Easy_Steps/Chapter2.ipynb
new file mode 100755
index 00000000..cd57ef03
--- /dev/null
+++ b/C++_Programming_In_Easy_Steps/Chapter2.ipynb
@@ -0,0 +1,419 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:1a5f4b4db0e022cc4962fee72ac059eb139d26a2030ea11b6f3c9bb4ceecd474"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 2 : Performing operations"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.1, Page No : 27"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "a = 8\n",
+ "b = 4\n",
+ "print \"Addition result: \",(a+b)\n",
+ "print \"Substraction result: \",(a-b)\n",
+ "print \"Multiplication result: \",(a*b)\n",
+ "print \"Division result: \",(a/b)\n",
+ "print \"Modulus result: \",(a%b)\n",
+ "print \"Postfix increment: \",a+1\n",
+ "print \"Postfix result: \",a\n",
+ "print \"Prefix increment: \",b+1\n",
+ "print \"Prefix result: \",b"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Addition result: 12\n",
+ "Substraction result: 4\n",
+ "Multiplication result: 32\n",
+ "Division result: 2\n",
+ "Modulus result: 0\n",
+ "Postfix increment: 9\n",
+ "Postfix result: 8\n",
+ "Prefix increment: 5\n",
+ "Prefix result: 4\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.2, Page No : 29"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "a = 8\n",
+ "b = 4\n",
+ "print \"Assigned values: \"\n",
+ "print \"a = \",a\n",
+ "print \"b = \",b\n",
+ "print \"Add & assign: \"\n",
+ "a = a + b\n",
+ "print \"a += b (8 += 4 ) a = \",a\n",
+ "print \"Subtract & assign: \"\n",
+ "a = a - b\n",
+ "print \"a -= b (12 -= 4 ) a = \",a\n",
+ "print \"Multiply & assign: \"\n",
+ "a = a * b\n",
+ "print \"a *= b (8 *= 4 ) a = \",a\n",
+ "print \"Divide & assign: \"\n",
+ "a = a / b\n",
+ "print \"a /= b (32 /= 4 ) a = \",a\n",
+ "print \"Modulus & assign: \"\n",
+ "a = a % b\n",
+ "print \"a %= b (8 %= 4 ) a = \",a"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Assigned values: \n",
+ "a = 8\n",
+ "b = 4\n",
+ "Add & assign: \n",
+ "a += b (8 += 4 ) a = 12\n",
+ "Subtract & assign: \n",
+ "a -= b (12 -= 4 ) a = 8\n",
+ "Multiply & assign: \n",
+ "a *= b (8 *= 4 ) a = 32\n",
+ "Divide & assign: \n",
+ "a /= b (32 /= 4 ) a = 8\n",
+ "Modulus & assign: \n",
+ "a %= b (8 %= 4 ) a = 0\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.3, Page No 31"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "nil= 0\n",
+ "num= 0\n",
+ "max1 = 1\n",
+ "cap= 'A'\n",
+ "low= 'a'\n",
+ "print \"Equality comparisons: \"\n",
+ "print \"(0 == 0) \",(nil == num),\"(true)\"\n",
+ "print \"(A == a) \",(cap == low),\"(false)\"\n",
+ "print \"Inequality comparison: \"\n",
+ "print \"(0 != 1) \",(nil != max1),\"(true)\"\n",
+ "print \"Greater comparison: \"\n",
+ "print \"(0 > 1)\",(nil > max1),\"(false)\"\n",
+ "print \"Lesser comparison: \"\n",
+ "print \"(0 < 1) \",(nil < max1),\"(true)\"\n",
+ "print \"Greater or equal comparison: \"\n",
+ "print \"(0 >= 0) \",(nil >= num),\"(true)\"\n",
+ "print \"Lesser or equal comparison: \"\n",
+ "print \"(1 <= 0) \",(max1 <= num),\"(false)\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Equality comparisons: \n",
+ "(0 == 0) True (true)\n",
+ "(A == a) False (false)\n",
+ "Inequality comparison: \n",
+ "(0 != 1) True (true)\n",
+ "Greater comparison: \n",
+ "(0 > 1) False (false)\n",
+ "Lesser comparison: \n",
+ "(0 < 1) True (true)\n",
+ "Greater or equal comparison: \n",
+ "(0 >= 0) True (true)\n",
+ "Lesser or equal comparison: \n",
+ "(1 <= 0) False (false)\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.4, Page No : 33"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "a = 1\n",
+ "b = 0\n",
+ "print \"AND logic: \"\n",
+ "print \"(a && a) \",(a & a),\"(true)\"\n",
+ "print \"(a && b) \",(a & b),\"(false)\"\n",
+ "print \"(b && b) \",(b & b),\"(false)\"\n",
+ "print \"OR logic: \"\n",
+ "print \"(a || a) \",( a | a ),\"(true)\"\n",
+ "print \"(a || b) \",( a | b ),\"(true)\"\n",
+ "print \"(b || b) \",( b | b ),\"(false)\"\n",
+ "print \"NOT logic: \"\n",
+ "print \"a = \",a,\"!a = \",not a\n",
+ "print \"b = \",b,\"!b = \",not b"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "AND logic: \n",
+ "(a && a) 1 (true)\n",
+ "(a && b) 0 (false)\n",
+ "(b && b) 0 (false)\n",
+ "OR logic: \n",
+ "(a || a) 1 (true)\n",
+ "(a || b) 1 (true)\n",
+ "(b || b) 0 (false)\n",
+ "NOT logic: \n",
+ "a = 1 !a = False\n",
+ "b = 0 !b = True\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Examle 2.5, Page No : 35"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "a = 1\n",
+ "b = 2\n",
+ "print \"Variable a value is: \"\n",
+ "if a != 1:\n",
+ " print \"not one\"\n",
+ "else:\n",
+ " print \"one\"\n",
+ "if a % 2 != 0:\n",
+ " print \"odd\"\n",
+ "else:\n",
+ " print \"even\"\n",
+ "print \"Variable b value is: \"\n",
+ "if b != 1:\n",
+ " print \"not one\"\n",
+ "else:\n",
+ " print \"one\"\n",
+ "if b % 2 != 0:\n",
+ " print \"odd\"\n",
+ "else:\n",
+ " print \"even\"\n",
+ "if a > b:\n",
+ " max1 = a\n",
+ "else:\n",
+ " max1 = b\n",
+ "print \"Greater value is \",max1"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Variable a value is: \n",
+ "one\n",
+ "odd\n",
+ "Variable b value is: \n",
+ "not one\n",
+ "even\n",
+ "Greater value is 2\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.6, Page No 37"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# there are no few datatypes in ipython so i assigned manually to get idea about size\n",
+ "import sys\n",
+ "num = 1\n",
+ "nums = range(50)\n",
+ "number = 0\n",
+ "max1 = 100\n",
+ "pi = 3.14\n",
+ "decimal = 10.10\n",
+ "letter = 'a'\n",
+ "letters = \"hii\"\n",
+ "isTrue = \"true\"\n",
+ "print \"int size: \",sys.getsizeof(num)\n",
+ "print \"50 int size: \",sys.getsizeof(nums)\n",
+ "print \"short int size: \",sys.getsizeof(number)\n",
+ "print \"unsigned int size: \",sys.getsizeof(max1)\n",
+ "print \"double size: \",sys.getsizeof(pi)\n",
+ "print \"float size: \",sys.getsizeof(decimal)\n",
+ "print \"char size: \",sys.getsizeof(letter)\n",
+ "print \"50 char size: \",sys.getsizeof(letters)\n",
+ "print \"bool size: \",sys.getsizeof(isTrue)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " int size: 24\n",
+ "50 int size: 464\n",
+ "short int size: 24\n",
+ "unsigned int size: 24\n",
+ "double size: 24\n",
+ "float size: 24\n",
+ "char size: 34\n",
+ "50 char size: 36\n",
+ "bool size: 37\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.7, Page No 39"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "num = 1 + 4 * 3\n",
+ "print \"Default order: \",num\n",
+ "num = (1 + 4) * 3\n",
+ "print \"Forced order: \",num\n",
+ "num = 7 - 4 + 2\n",
+ "print \"Default direction: \",num\n",
+ "num = 7 - (4 + 2)\n",
+ "print \"Forced direction: \",num"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Default order: 13\n",
+ "Forced order: 15\n",
+ "Default direction: 5\n",
+ "Forced direction: 1\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 2.8, Page No 41"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "num= 7\n",
+ "factor= 2\n",
+ "letter= 'A'\n",
+ "result= 0.0;\n",
+ "print \"Integer division: \",(num/factor)\n",
+ "result = float(num) / factor\n",
+ "print \"Cast division float: \",result\n",
+ "num = ord(letter)\n",
+ "print \"Cast character int: \",num\n",
+ "letter = chr(70)\n",
+ "print \"Cast integer char: \",letter"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Integer division: 3\n",
+ "Cast division float: 3.5\n",
+ "Cast character int: 65\n",
+ "Cast integer char: F\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file