summaryrefslogtreecommitdiff
path: root/Programming_With_Java_A_Primer_by_E._Balagurusamy/chapter6.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Programming_With_Java_A_Primer_by_E._Balagurusamy/chapter6.ipynb')
-rwxr-xr-xProgramming_With_Java_A_Primer_by_E._Balagurusamy/chapter6.ipynb245
1 files changed, 245 insertions, 0 deletions
diff --git a/Programming_With_Java_A_Primer_by_E._Balagurusamy/chapter6.ipynb b/Programming_With_Java_A_Primer_by_E._Balagurusamy/chapter6.ipynb
new file mode 100755
index 00000000..3ffd656c
--- /dev/null
+++ b/Programming_With_Java_A_Primer_by_E._Balagurusamy/chapter6.ipynb
@@ -0,0 +1,245 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:1252446619e6e4563a32f0df79325967492fbf5a5dc688c7dbacba6bd7f62327"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 6: Decision Making & Branching"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 6.1, page no. 84"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "weight = [45.0, 55.0, 47.0, 51.0, 54.0]\n",
+ "height = [176.5, 174.2, 168.0, 170.7, 169.0]\n",
+ "count = 0\n",
+ "count1 = 0\n",
+ "for i in range(0, 5):\n",
+ " if(weight[i]<50.0 and height[i]>170.0):\n",
+ " count1 += 1\n",
+ " count += 1\n",
+ "count2 = count - count1\n",
+ "print \"Number of persons with...\"\n",
+ "print \"Weight<50 and height>170 = \", count1\n",
+ "print \"Others = \", count2"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Number of persons with...\n",
+ "Weight<50 and height>170 = 1\n",
+ "Others = 4\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 6.2, page no. 86"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "number = [50, 65, 56, 71, 81]\n",
+ "even = 0\n",
+ "odd = 0\n",
+ "for i in range(0, len(number)):\n",
+ " if(number[i] % 2 == 0):\n",
+ " even += 1\n",
+ " else:\n",
+ " odd += 1\n",
+ "print \"Even numbers = \", even, \"Odd Numbers = \", odd"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Even numbers = 2 Odd Numbers = 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 6.3, page no. 89"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "a = 325\n",
+ "b = 712\n",
+ "c = 478\n",
+ "\n",
+ "print \"Largets value is \",\n",
+ "if a > b:\n",
+ " if a > c:\n",
+ " print a\n",
+ " else:\n",
+ " print c\n",
+ "else:\n",
+ " if c > b:\n",
+ " print c\n",
+ " else:\n",
+ " print b"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Largets value is 712\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 6.4, page no. 92"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "rollnumber = [111, 222, 333, 444]\n",
+ "marks = [81, 75, 43, 58]\n",
+ "for i in range(0, len(rollnumber)):\n",
+ " if marks[i] > 79:\n",
+ " print rollnumber[i], \"Honours\"\n",
+ " elif marks[i] > 59:\n",
+ " print rollnumber[i], \"I Division\"\n",
+ " elif marks[i] > 49:\n",
+ " print rollnumber[i], \"II Division\"\n",
+ " else:\n",
+ " print rollnumber[i], \"Fail\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "111 Honours\n",
+ "222 I Division\n",
+ "333 Fail\n",
+ "444 II Division\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 6.5, page no. 96"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "#there is switch() statement in python, we will use if...else instead\n",
+ "\n",
+ "print \"Select your choice: \"\n",
+ "print \"M --> Madras\"\n",
+ "print \"B --> Bombay\"\n",
+ "print \"C --> Calcutta\"\n",
+ "print \"Choice --> \",\n",
+ "choice = raw_input()\n",
+ "if choice == \"M\" or choice == \"m\":\n",
+ " print \"Madras: Booklet 5\"\n",
+ "elif choice == \"B\" or choice == \"b\":\n",
+ " print \"Bombay: Booklet 9\"\n",
+ "elif choice == \"C\" or choice == \"c\":\n",
+ " print \"Calcutta: Booklet 15\"\n",
+ "else:\n",
+ " print \"Invalid Choice (IC)\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Select your choice: \n",
+ "M --> Madras\n",
+ "B --> Bombay\n",
+ "C --> Calcutta\n",
+ "Choice --> "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "m\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Madras: Booklet 5\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file