"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Print the first five numbers starting from one together with thier squares\n",
"\n",
"#Result\n",
"#Since range() function starts with 0; to print upto 5, use 5+1 or 6\n",
"#range() function creates range or numbers from 1 to 6\n",
"\n",
"for i in range(1,5+1):\n",
" print(\"Number : %d it's Square : %d\"%(i,i*i))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Number : 1 it's Square : 1\n",
"Number : 2 it's Square : 4\n",
"Number : 3 it's Square : 9\n",
"Number : 4 it's Square : 16\n",
"Number : 5 it's Square : 25\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.2, Page number: 108
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display numbers from 1 to 15 using for loop. Use i++\n",
"\n",
"import sys\n",
"\n",
"#Result\n",
"#There is no increment operator (++) in python\n",
"#the range() function creates range of numbers and for loop automatically\n",
"#iterate through the numbers\n",
"\n",
"for i in range(1,15+1):\n",
" sys.stdout.write(\"%5d\"%(i))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.3, Page number: 108
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display numbers from 1 to 15 using for loop. Use i = i + 1\n",
"\n",
"import sys\n",
"\n",
"#Result\n",
"#the range() function creates range of numbers and for loop automatically\n",
"#iterate through the numbers\n",
"\n",
"for i in range(1,15+1):\n",
" sys.stdout.write(\"%5d\"%(i))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.4, Page number: 109
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display numbers from 1 to 16. Use incrementation operation in the body of\n",
"#the loop for more than once\n",
"\n",
"import sys\n",
"\n",
"#Variable declaration\n",
"\n",
"c = 0\n",
"\n",
"#Result\n",
"#the range() function creates range of numbers and for loop automatically\n",
"#iterate through the numbers and the third argument in the range function\n",
"#indicates the interval between the numbers.\n",
"\n",
"for i in range(0,15+1,2):\n",
" i += 1\n",
" sys.stdout.write(\"%5d\"%(i))\n",
" i = i + 1\n",
" sys.stdout.write(\"%5d\"%(i))\n",
" c += 1\n",
"\n",
"sys.stdout.write(\"\\n\\nThe Body of the loop is executed for %d times\"%(c))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16\n",
"\n",
"The Body of the loop is executed for 8 times"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.5, Page number: 109
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display even numbers from 0 to 14 by declaring the initial counter value before\n",
"#the loop statement\n",
"import sys\n",
"\n",
"#Variable declaration\n",
"\n",
"c = 0\n",
"i = 0\n",
"\n",
"#Result\n",
"#the range() function creates range of numbers and for loop automatically\n",
"#iterate through the numbers and the third argument in the range function\n",
"#indicates the interval between the numbers.\n",
"\n",
"#counter variable initialization before the for loop will not affect much.\n",
"\n",
"for i in range(0,15+1,2):\n",
" sys.stdout.write(\"%5d\"%(i))\n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
" 0 2 4 6 8 10 12 14"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.6, Page number: 110
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display numbers in ascending and descending orders.\n",
"\n",
"import sys\n",
"\n",
"#Variable declaration\n",
"\n",
"i = 0\n",
"\n",
"#Result\n",
"#the range() function creates range of numbers and for loop automatically\n",
"#iterate through the numbers and the third argument in the range function\n",
"#indicates the interval between the numbers.\n",
"\n",
"#counter variable initialization before the for loop will not affect much.\n",
"\n",
"sys.stdout.write(\"Numbers in Ascending Order : \")\n",
"for i in range(1,10+1):\n",
" sys.stdout.write(\"%3d\"%(i))\n",
" \n",
"sys.stdout.write(\"\\nNumbers in Descending Order : \")\n",
"for i in range(10,0,-1):\n",
" sys.stdout.write(\"%3d\"%(i))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Numbers in Ascending Order : 1 2 3 4 5 6 7 8 9 10\n",
"Numbers in Descending Order : 10 9 8 7 6 5 4 3 2 1"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.7, Page number: 110
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display numbers upto 10 using infinite for loop. Use goto statement to\n",
"#exit from the loop\n",
"\n",
"import sys\n",
"\n",
"#Variable declaration\n",
"\n",
"i = 0\n",
"\n",
"#Result\n",
"#the range() function creates range of numbers and for loop automatically\n",
"#iterate through the numbers and the third argument in the range function\n",
"#indicates the interval between the numbers.\n",
"\n",
"#counter variable initialization before the for loop will not affect much.\n",
"\n",
"for i in range(0,15):\n",
" sys.stdout.write(\"%3d\"%(i))\n",
" i += 1\n",
" if i == 11:\n",
" break\n",
"\n",
"#there is no infinite for loop and goto statement in python.\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
" 0 1 2 3 4 5 6 7 8 9 10"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.8, Page number: 111
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Count numbers between 1 to 100 not divisible by 2, 3, and 5\n",
"\n",
"import sys\n",
"\n",
"#Variable declaration\n",
"\n",
"c = 0\n",
"\n",
"sys.stdout.write(\"\\nNumbers from 1 to 100 not divisible by 2,3&5\\n\\n\")\n",
"\n",
"#Result\n",
"#the range() function creates range of numbers and for loop automatically\n",
"#iterate through the numbers \n",
"\n",
"for x in range(0,100+1):\n",
" if x%2 != 0 and x%3 != 0 and x%5 != 0:\n",
" sys.stdout.write(\"%d\\t\"%(x))\n",
" c += 1\n",
"\n",
"sys.stdout.write(\"\\nTotal Numbers : %d\"%(c))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Numbers from 1 to 100 not divisible by 2,3&5\n",
"\n",
"1\t7\t11\t13\t17\t19\t23\t29\t31\t37\t41\t43\t47\t49\t53\t59\t61\t67\t71\t73\t77\t79\t83\t89\t91\t97\t\n",
"Total Numbers : 26"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.9, Page number: 112
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display the numbers in increasing and decreasing order using infinite\n",
"#for loop\n",
"\n",
"import sys\n",
"\n",
"#Variable declaration\n",
"n = int(raw_input(\"Enter a number : \"))\n",
"a = b = n\n",
"\n",
"sys.stdout.write(\"(++) (--)\\n\")\n",
"sys.stdout.write(\"===================\")\n",
"\n",
"#Result\n",
"#the range() function creates range of numbers and for loop automatically\n",
"#iterate through the numbers\n",
"\n",
"#There is no infinite for loop in python. so separate iteration variable i is used\n",
"\n",
"for i in range(0,100):\n",
" sys.stdout.write(\"\\n%d\\t%d\"%(a,b))\n",
" if b == 0:\n",
" break\n",
" a += 1\n",
" b -= 1\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a number : 5\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"(++) (--)\n",
"===================\n",
"5\t5\n",
"6\t4\n",
"7\t3\n",
"8\t2\n",
"9\t1\n",
"10\t0"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.10, Page number: 113
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Create an infinite loop. Check each value of the for loop. if the value is\n",
"#even, display it otherwise continue with interations. Print even numbers from\n",
"#1 to 21. Use break statement to terminate the program.\n",
"\n",
"import sys\n",
"\n",
"#Variable declaration\n",
"i = 1\n",
"\n",
"sys.stdout.write(\"\\n\\t\\tTable of Even numbers from 1 to 20\")\n",
"sys.stdout.write(\"\\n\\t\\t==================================\\n\")\n",
"\n",
"#Result\n",
"#the range() function creates range of numbers and for loop automatically\n",
"#iterate through the numbers\n",
"\n",
"#There is no infinite for loop in python. so separate iteration variable i is used\n",
"\n",
"for i in range(1,100):\n",
" if i == 20:\n",
" break\n",
" else:\n",
" if i%2 == 0:\n",
" sys.stdout.write(\"%d\\t\"%(i))\n",
" continue\n",
" else:\n",
" continue"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\t\tTable of Even numbers from 1 to 20\n",
"\t\t==================================\n",
"2\t4\t6\t8\t10\t12\t14\t16\t18\t"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.11, Page number: 113
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Calculate the sum of first five numbers and their squares. Display their results\n",
"\n",
"import sys\n",
"\n",
"#Variable declaration\n",
"#since sum is a function in python, sum1 is used instead of variable sum.\n",
"\n",
"sum1 = 0\n",
"sqsum = 0\n",
"\n",
"#Result\n",
"#the range() function creates range of numbers and for loop automatically\n",
"#iterate through the numbers\n",
"\n",
"for i in range(1,5+1):\n",
" sum1 += i\n",
" sqsum += i*i\n",
" sys.stdout.write(\"\\nNumber : %5d it's Square : %8d\"%(i,i*i))\n",
"\n",
"sys.stdout.write(\"\\n=================================================\")\n",
"sys.stdout.write(\"\\nThe Sum %6d Sum of Squares %9d\"%(sum1,sqsum))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Number : 1 it's Square : 1\n",
"Number : 2 it's Square : 4\n",
"Number : 3 it's Square : 9\n",
"Number : 4 it's Square : 16\n",
"Number : 5 it's Square : 25\n",
"=================================================\n",
"The Sum 15 Sum of Squares 55"
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.12, Page number: 114
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display numbers from 1 to 9 and their square roots.\n",
"\n",
"import math\n",
"import sys\n",
"\n",
"#Result\n",
"#the range() function creates range of numbers and for loop automatically\n",
"#iterate through the numbers\n",
"\n",
"for i in range(1,9+1):\n",
" a = math.sqrt(i)\n",
" sys.stdout.write(\"\\n\\t%d %.2f\"%(i,a))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\t1 1.00\n",
"\t2 1.41\n",
"\t3 1.73\n",
"\t4 2.00\n",
"\t5 2.24\n",
"\t6 2.45\n",
"\t7 2.65\n",
"\t8 2.83\n",
"\t9 3.00"
]
}
],
"prompt_number": 15
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.13, Page number: 115
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Find the number in between 7 and 100 which is exactly divisible by\n",
"#4 and if divided by 5 and 6 remainders obtained should be 4.\n",
"\n",
"import sys\n",
"\n",
"#Result\n",
"#the range() function creates range of numbers and for loop automatically\n",
"#iterate through the numbers\n",
"\n",
"for x in range(7,100+1):\n",
" if x%4 == 0 and x%5 == 4 and x%6 == 4:\n",
" sys.stdout.write(\"\\nMinimum Number : %d\"%(x))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Minimum Number : 64"
]
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.14, Page number: 115
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Evaluate the series given\n",
"# x = 1/1 + 1/4 + 1/9 ... 1/n2\n",
"# y = 1/1 + 1/8 + 1/27 ... 1/n3\n",
"\n",
"#Variable declaration\n",
"x = 0.0\n",
"y = 0.0\n",
"n = int(raw_input(\"Enter Value of n : \"))\n",
"\n",
"#Result\n",
"#the range() function creates range of numbers and for loop automatically\n",
"#iterate through the numbers\n",
"\n",
"for i in range(1,n+1):\n",
" x = x + (1.0/pow(i,2))\n",
" y = y + (1.0/pow(i,3))\n",
"\n",
"print(\"Value of x = %f\"%(x))\n",
"print(\"\\nValue of y = %f\"%(y))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Value of n : 2\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Value of x = 1.250000\n",
"\n",
"Value of y = 1.125000\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.15, Page number: 116
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Generate Triangular number\n",
"#Note: Triangular number is nothing but summation of 1 to given number.\n",
"#For example, when entered number is 5 it's triangular number would be\n",
"#(1+2+3+4+5) = 15.\n",
"\n",
"\n",
"#Variable declaration\n",
"tri_num = 0\n",
"n = int(raw_input(\"What Triangular number do you want : \"))\n",
"\n",
"#Result\n",
"#the range() function creates range of numbers and for loop automatically\n",
"#iterate through the numbers\n",
"\n",
"for j in range(1,n+1):\n",
" tri_num = tri_num + j\n",
"\n",
"print(\"Triangular number of %d is %d\\n\"%(n,tri_num))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"What Triangular number do you want : 5\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Triangular number of 5 is 15\n",
"\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.16, Page number: 117
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Find sum of the following series\n",
"# 1 + 2 + 3 + ... n\n",
"# 1^2 + 2^2 + 2^3 + ... n^2\n",
"\n",
"import sys\n",
"\n",
"#Variable declaration\n",
"#since sum is a function name in python, sum1 is used instead of sum.\n",
"\n",
"sum1 = 0\n",
"ssum = 0\n",
"j = int(raw_input(\"Enter Number : \"))\n",
"\n",
"#Result\n",
"#the range() function creates range of numbers and for loop automatically\n",
"#iterate through the numbers\n",
"\n",
"sys.stdout.write(\"Numbers : \")\n",
"for i in range(1,j+1):\n",
" sys.stdout.write(\"%5d\"%(i))\n",
"\n",
"sys.stdout.write(\"\\nSquares : \")\n",
"for i in range(1,j+1):\n",
" sys.stdout.write(\"%5d\"%(i*i))\n",
" sum1 = sum1 + i\n",
" ssum = ssum + i*i\n",
"sys.stdout.write(\"\\nSum of Numbers from 1 to %d : %d\"%(j,sum1))\n",
"sys.stdout.write(\"\\nSum of Squares of 1 to %d Numbers : %d\"%(j,ssum))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Number : 5\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Numbers : 1 2 3 4 5\n",
"Squares : 1 4 9 16 25\n",
"Sum of Numbers from 1 to 5 : 15\n",
"Sum of Squares of 1 to 5 Numbers : 55"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.17, Page number: 118
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display the perfect squares from 1 to 500.\n",
"\n",
"import sys\n",
"import math\n",
"\n",
"#Result\n",
"#the range() function creates range of numbers and for loop automatically\n",
"#iterate through the numbers\n",
"\n",
"sys.stdout.write(\"\\n\\n\")\n",
"sys.stdout.write(\"Perfect square from 1 to 500 \\n\")\n",
"count = 0\n",
"\n",
"for i in range(1,500+1):\n",
" c = math.sqrt(i)\n",
" x = math.floor(c) #For rounding up floor() is used\n",
" if c == x:\n",
" sys.stdout.write(\"\\t%5d\"%(i))\n",
" count += 1\n",
"\n",
"sys.stdout.write(\"\\n\\nTotal Perfect Squares = %d\"%(count))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
"Perfect square from 1 to 500 \n",
"\t 1\t 4\t 9\t 16\t 25\t 36\t 49\t 64\t 81\t 100\t 121\t 144\t 169\t 196\t 225\t 256\t 289\t 324\t 361\t 400\t 441\t 484\n",
"\n",
"Total Perfect Squares = 22"
]
}
],
"prompt_number": 20
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.18, Page number: 119
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Detect the largest number out of five numbers.\n",
"\n",
"import sys\n",
"import math\n",
"\n",
"#Result\n",
"#the range() function creates range of numbers and for loop automatically\n",
"#iterate through the numbers.\n",
"#since sum is a built in function in python, sum1 is used instead of sum.\n",
"\n",
"a = int(raw_input(\"Enter five numbers : \"))\n",
"b = int(raw_input(\"Enter five numbers : \"))\n",
"c = int(raw_input(\"Enter five numbers : \"))\n",
"d = int(raw_input(\"Enter five numbers : \"))\n",
"e = int(raw_input(\"Enter five numbers : \"))\n",
"\n",
"sum1 = a + b + c + d + e\n",
"\n",
"for i in range(sum1,1,-1):\n",
" if i == a or i == b or i == c or i == d or i == e:\n",
" sys.stdout.write(\"The Largest Number : %d\"%(i))\n",
" break\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter five numbers : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter five numbers : 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter five numbers : 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter five numbers : 7\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter five numbers : 3\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The Largest Number : 7"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.19, Page number: 119
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Detect the smallesst number out of five numbers.\n",
"\n",
"import sys\n",
"import math\n",
"\n",
"#Result\n",
"#the range() function creates range of numbers and for loop automatically\n",
"#iterate through the numbers.\n",
"#since sum is a built in function in python, sum1 is used instead of sum.\n",
"\n",
"a = int(raw_input(\"Enter five numbers : \"))\n",
"b = int(raw_input(\"Enter five numbers : \"))\n",
"c = int(raw_input(\"Enter five numbers : \"))\n",
"d = int(raw_input(\"Enter five numbers : \"))\n",
"e = int(raw_input(\"Enter five numbers : \"))\n",
"\n",
"sum1 = a + b + c + d + e\n",
"\n",
"for i in range(1,sum1):\n",
" if i == a or i == b or i == c or i == d or i == e:\n",
" sys.stdout.write(\"The Smallest Number : %d\"%(i))\n",
" break\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter five numbers : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter five numbers : 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter five numbers : 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter five numbers : 7\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter five numbers : 3\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The Smallest Number : 2"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.20, Page number: 120
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Read five numbers and print in ascending order\n",
"\n",
"import sys\n",
"import math\n",
"\n",
"#Result\n",
"#the range() function creates range of numbers and for loop automatically\n",
"#iterate through the numbers.\n",
"#since sum is a built in function in python, sum1 is used instead of sum.\n",
"\n",
"#use commas to separate the input values\n",
"a = int(raw_input(\"Enter five numbers : \"))\n",
"b = int(raw_input(\"Enter five numbers : \"))\n",
"c = int(raw_input(\"Enter five numbers : \"))\n",
"d = int(raw_input(\"Enter five numbers : \"))\n",
"e = int(raw_input(\"Enter five numbers : \"))\n",
"\n",
"sys.stdout.write(\"Numbers in ascending order : \")\n",
"sum1 = a + b + c + d + e\n",
"\n",
"for i in range(1,sum1):\n",
" if i == a or i == b or i == c or i == d or i == e:\n",
" sys.stdout.write(\"%3d\"%(i))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter five numbers : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter five numbers : 8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter five numbers : 7\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter five numbers : 4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter five numbers : 1\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Numbers in ascending order : 1 4 5 7 8"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.21, Page number: 121
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Perform multiplication of two integers by using negative sign\n",
"\n",
"import sys\n",
"\n",
"#Result\n",
"#the range() function creates range of numbers and for loop automatically\n",
"#iterate through the numbers.\n",
"\n",
"a = int(raw_input(\"Enter two numbers : \"))\n",
"b = int(raw_input(\"Enter two numbers : \"))\n",
"\n",
"d = 0\n",
"for c in range(1,b+1):\n",
" d = d - (-a)\n",
"\n",
"sys.stdout.write(\"Multiplication of %d * %d : %d\"%(a,b,d))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter two numbers : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter two numbers : 5\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Multiplication of 5 * 5 : 25"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.22, Page number: 121
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Perform multiplication of two integers by using repetitive addition\n",
"\n",
"import sys\n",
"\n",
"#Result\n",
"#in python, infinite for loop is not possible.\n",
"\n",
"a = int(raw_input(\"Enter two numbers : \"))\n",
"b = int(raw_input(\"Enter two numbers : \"))\n",
"\n",
"d = 0\n",
"c = 1\n",
"while True:\n",
" d = d + a\n",
" if c == b:\n",
" break\n",
" c += 1\n",
"\n",
"sys.stdout.write(\"Multiplication of %d * %d : %d\"%(a,b,d))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter two numbers : 8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter two numbers : 4\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Multiplication of 8 * 4 : 32"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.23, Page number: 122
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Read the marks for five subjects and calculate sum & average of marks.\n",
"\n",
"import sys\n",
"\n",
"#Result\n",
"#use spaces to separate the input values\n",
"#since sum is a built-in function in python, sum1 is used as the variable name\n",
"\n",
"sys.stdout.write(\"Enter The Marks of Five Subjects \")\n",
"sum1 = 0\n",
"\n",
"for i in range(1,5+1):\n",
" sys.stdout.write(\"[%d] Student : \"%(i))\n",
" #Exception handling\n",
" try:\n",
" #use space to separate the input characters\n",
" v = raw_input(\"\")\n",
" a,b,c,d,e = map(int,v.split())\n",
" x = 5\n",
" except:\n",
" x = 0\n",
"\n",
" if(x == 5):\n",
" sum1 = a + b + c + d + e\n",
" avg = sum1/5\n",
" sys.stdout.write(\"\\nTotal Marks of Student [%d] %d\"%(i,sum1))\n",
" sys.stdout.write(\"\\nAverage Marks of Student [%d] %f\"%(i,avg))\n",
" else:\n",
" sys.stdout.write(\"Type Mismatch\")\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter The Marks of Five Subjects [1] Student : "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"58 52 52 56 78\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Total Marks of Student [1] 296\n",
"Average Marks of Student [1] 59.000000[2] Student : "
]
}
],
"prompt_number": "*"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.24, Page number: 123
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Enter a character and display its position in alphabetic order.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"c = 1\n",
"\n",
"ch = raw_input(\"Enter a character :\")\n",
"ch = ch.upper()\n",
"\n",
"#Result\n",
"\n",
"#ord() function converts the character ASCII value to integer\n",
"for i in range(65,90+1):\n",
" if ord(ch) == i:\n",
" sys.stdout.write(\"'%c' is %d [st/nd/rd/th] Character in Alphabetic\"%(i,c))\n",
" c += 1\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a character :U\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"'U' is 21 [st/nd/rd/th] Character in Alphabetic"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.25, Page number: 124
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Calculate the value of -m^x.\n",
"#'m' is a mantissa and 'x' is an exponent\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"number = int(raw_input(\"Enter Number : \"))\n",
"power = int(raw_input(\"Enter Power : \"))\n",
"\n",
"ans = 1\n",
"\n",
"#Calculation\n",
"for i in range(1,power+1):\n",
" ans *= number\n",
"\n",
"#Result\n",
"sys.stdout.write(\"The Power of %d raised to %d is %ld\"%(number,power,ans))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Number : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Power : 3\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The Power of 5 raised to 3 is 125"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.26, Page number: 124
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Subtraction of two loop variables using nested for loops.\n",
"\n",
"import sys\n",
"\n",
"#Calculation & Result\n",
"for a in range(3,0,-1):\n",
" for b in range(1,2+1):\n",
" sub = a - b\n",
" sys.stdout.write(\"a = %d b = %d a - b = %d\\n\"%(a,b,sub))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"a = 3 b = 1 a - b = 2\n",
"a = 3 b = 2 a - b = 1\n",
"a = 2 b = 1 a - b = 1\n",
"a = 2 b = 2 a - b = 0\n",
"a = 1 b = 1 a - b = 0\n",
"a = 1 b = 2 a - b = -1\n"
]
}
],
"prompt_number": 31
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.27, Page number: 125
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Illustrate an example based on nested for loops\n",
"\n",
"import sys\n",
"\n",
"#Calculation & Result\n",
"for i in range(1,3+1): #outer loop\n",
" for j in range(1,2+1): #inner loop\n",
" sys.stdout.write(\"\\ni * j : %d\"%(i*j))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"i * j : 1\n",
"i * j : 2\n",
"i * j : 2\n",
"i * j : 4\n",
"i * j : 3\n",
"i * j : 6"
]
}
],
"prompt_number": 32
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.28, Page number: 126
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Print values and messages when any loop ends using nested for loops.\n",
"\n",
"import sys\n",
"\n",
"#Calculation & Result\n",
"for a in range(1,2+1): #outer loop\n",
" for b in range(1,2+1): #middle loop\n",
" for c in range(1,2+1): #inner loop\n",
" sys.stdout.write(\"\\na = %d + b = %d + c = %d : %d\"%(a,b,c,a+b+c))\n",
" sys.stdout.write(\"\\nInner Loop Over.\")\n",
" sys.stdout.write(\"\\nMiddle Loop Over.\")\n",
"sys.stdout.write(\"\\nOuter Loop Over.\")\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"a = 1 + b = 1 + c = 1 : 3\n",
"a = 1 + b = 1 + c = 2 : 4\n",
"Inner Loop Over.\n",
"a = 1 + b = 2 + c = 1 : 4\n",
"a = 1 + b = 2 + c = 2 : 5\n",
"Inner Loop Over.\n",
"Middle Loop Over.\n",
"a = 2 + b = 1 + c = 1 : 4\n",
"a = 2 + b = 1 + c = 2 : 5\n",
"Inner Loop Over.\n",
"a = 2 + b = 2 + c = 1 : 5\n",
"a = 2 + b = 2 + c = 2 : 6\n",
"Inner Loop Over.\n",
"Middle Loop Over.\n",
"Outer Loop Over."
]
}
],
"prompt_number": 33
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Count occurrence of 0 to 9 digits between 1 and given decimal number\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"n = int(raw_input(\"Enter a Decimal Number : \"))\n",
"st = [0 for x in range(10)] #10 array elements initialized as 0\n",
"\n",
"#Calculation\n",
"for l in range(1,n+1):\n",
" t = l\n",
" while t != 0:\n",
" k = t % 10\n",
" t = t / 10\n",
" st[k] += 1\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\nOccurrence of 0-9 digits between 1 to %d Numbers.\"%(n))\n",
"sys.stdout.write(\"\\n========== == === ====== ======= = == == ========\")\n",
"\n",
"for i in range(0,10):\n",
" if st[i] > 0:\n",
" sys.stdout.write(\"\\n%d Occurs %8d Times.\"%(i,st[i]))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a Decimal Number : 15\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Occurrence of 0-9 digits between 1 to 15 Numbers.\n",
"========== == === ====== ======= = == == ========\n",
"0 Occurs 1 Times.\n",
"1 Occurs 8 Times.\n",
"2 Occurs 2 Times.\n",
"3 Occurs 2 Times.\n",
"4 Occurs 2 Times.\n",
"5 Occurs 2 Times.\n",
"6 Occurs 1 Times.\n",
"7 Occurs 1 Times.\n",
"8 Occurs 1 Times.\n",
"9 Occurs 1 Times."
]
}
],
"prompt_number": 20
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.34, Page number: 132
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display sum of digits of a given number\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"s = 0\n",
"n = int(raw_input(\"Enter a Number : \"))\n",
"\n",
"#Calculation & Result\n",
"\n",
"sys.stdout.write(\"\\nSum of Digits till a single digit\\n %d\"%(n))\n",
"\n",
"while n != 0:\n",
" s = s + n%10\n",
" n = n/10\n",
" if n == 0 and s > 9:\n",
" sys.stdout.write(\"\\n %2d\"%(s))\n",
" n = s\n",
" s = 0\n",
"\n",
"sys.stdout.write(\"\\n %2d\"%(s))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a Number : 4687\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Sum of Digits till a single digit\n",
" 4687\n",
" 25\n",
" 7"
]
}
],
"prompt_number": 21
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.35, Page number: 133
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display octal numbers in binary. Attach a parity bit with \"1\"\n",
"#if number of 1s are even otherwise \"0\".\n",
"# OR\n",
"#Generate odd parity to octal numbers 0 to 7. Express each number in binary\n",
"#and attach the parity bit\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"c = 0\n",
"j = 12\n",
"k = 2\n",
"\n",
"#Calculation & Result\n",
"\n",
"sys.stdout.write(\"\\nBinary Bits Parity Bits\")\n",
"sys.stdout.write(\"\\n============= ==============\\n\")\n",
"\n",
"for x in range(0,8):\n",
" k += 1\n",
" j = 12\n",
" y = x\n",
" for a in range(0,3):\n",
" b = y % 2\n",
" #gotoxy\n",
" sys.stdout.write(\"\\t%d\"%(b))\n",
" y = y / 2\n",
" if b == 1:\n",
" c += 1\n",
" if c%2 == 0:\n",
" #gotoxy\n",
" sys.stdout.write(\"\\t1\")\n",
" else:\n",
" #gotosy\n",
" sys.stdout.write(\"\\t0\\n\")\n",
" c = 0\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Binary Bits Parity Bits\n",
"============= ==============\n",
"\t0\t0\t0\t1\t1\t0\t0\t0\n",
"\t0\t1\t0\t0\n",
"\t1\t1\t0\t1\t0\t0\t1\t0\n",
"\t1\t0\t1\t1\t0\t1\t1\t1\t1\t1\t1\t0\n"
]
}
],
"prompt_number": 22
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.36, Page number: 135
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Evaluate the series x - x^3/3! + x^5/5! - .... x^n/n!\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"c = 3.0\n",
"f = 1.0\n",
"\n",
"#Give x as float value and n as int value\n",
"\n",
"x = float(raw_input(\"Enter x & n : \")) #use commas to input values\n",
"n = int(raw_input(\"Enter x & n : \")) \n",
"\n",
"sum1 = x #since sum is built-in function in python,\n",
" #sum1 is used instead of sum\n",
"\n",
"#Calculation & Result\n",
"\n",
"for i in range(3,n+1,2):\n",
" f = 1\n",
" if c%2 != 0:\n",
" for l in range(1,i+1):\n",
" f = f * l\n",
" sum1 = sum1 - pow(x,i)/ f\n",
" else:\n",
" for l in range(1,i+1):\n",
" f = f * l\n",
" sum1 = sum1 + pow(x,i)/ f\n",
" c += 1\n",
"\n",
"sys.stdout.write(\"\\nSum of series Numbers : %f\"%(sum1))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter x & n : 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter x & n : 5\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Sum of series Numbers : 0.933333"
]
}
],
"prompt_number": 24
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.37, Page number: 136
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Evaluate the series x + x^2/2! + x^4/4! + .... x^n/n!\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"f = 1.0\n",
"\n",
"#Give x as float value and y as int value\n",
"\n",
"x = float(raw_input(\"Enter x & y : \")) #use commas to input values\n",
"y = int(raw_input(\"Enter x & y : \")) \n",
"\n",
"sum1 = x #since sum is built-in function in python,\n",
" #sum1 is used instead of sum\n",
"\n",
"#Calculation & Result\n",
"\n",
"for i in range(2,y+1,2):\n",
" f = 1\n",
" for l in range(1,i+1):\n",
" f = f * l\n",
" sum1 = sum1 + pow(x,i)/ f\n",
"\n",
"sys.stdout.write(\"\\nSum of Series : %f\"%(sum1))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter x & y : 4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter x & y : 4\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Sum of Series : 22.666667"
]
}
],
"prompt_number": 25
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.38, Page number: 136
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Evaluate the series 1 - 1/1! + 2/2! - 3/3! .... n/n!\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"c = 3\n",
"\n",
"n = int(raw_input(\"Enter value of n : \"))\n",
"\n",
"sum1 = 1.0 #since sum is built-in function in python,\n",
" #sum1 is used instead of sum\n",
"\n",
"#Calculation\n",
"\n",
"for i in range(1,n+1):\n",
" f = 1.0\n",
" if c%2 != 0:\n",
" for l in range(1,i+1):\n",
" f = f * l\n",
" k = float(i / f)\n",
" sum1 = sum1 - k\n",
" else:\n",
" for l in range(1,i+1):\n",
" f = f * l\n",
" sum1 = sum1 + float(i/f)\n",
" c += 1\n",
" \n",
"#Result\n",
"sys.stdout.write(\"\\nSum of series Numbers : %f\"%(sum1))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter value of n : 3\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Sum of series Numbers : 0.500000"
]
}
],
"prompt_number": 31
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.39, Page number: 137
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display the Armstrong numbers in three digits from 100 to 999. If sum of cubes of each digits\n",
"#of the number is equal to number itself, then the number is called as an Armstrong number.\n",
"#(For eg. 153 = 1^3 + 5^3 + 3^3 = 153)\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"cube = 0\n",
"\n",
"sys.stdout.write(\"The Following Numbers are Armstrong numbers.\")\n",
"\n",
"#Calculation & Result\n",
"\n",
"for k in range(100,999+1):\n",
" cube = 0\n",
" x = 1\n",
" d = 3\n",
" n = k\n",
" while x <= d:\n",
" i = n % 10\n",
" cube = cube + pow(i,3)\n",
" n = n / 10\n",
" x += 1\n",
" if cube == k:\n",
" sys.stdout.write(\"\\n\\t%d\"%(k))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The Following Numbers are Armstrong numbers.\n",
"\t153\n",
"\t370\n",
"\t371\n",
"\t407"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.40, Page number: 138
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Example 6.40.py\n",
"#Program to display the stars as shown below\n",
"#*\n",
"#**\n",
"#***\n",
"#****\n",
"#*****\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"\n",
"x = int(raw_input(\"How many lines stars (*) should be printed ? \"))\n",
"\n",
"#Result\n",
"\n",
"for i in range(1,x+1):\n",
" for j in range(1,i+1):\n",
" sys.stdout.write(\"*\")\n",
" sys.stdout.write(\"\\n\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"How many lines stars (*) should be printed ? 5\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"*\n",
"**\n",
"***\n",
"****\n",
"*****\n"
]
}
],
"prompt_number": 32
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Convert binary to decimal number\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"x = [1 for i in range(1,5+1)] #defines the array x and initializes the elements with 0\n",
"y = x[0]\n",
"\n",
"sys.stdout.write(\"\\nValues in different Iterations\")\n",
"sys.stdout.write(\"\\n====== == ========= ==========\\n\")\n",
"\n",
"for i in range(0,4):\n",
" y = y * 2 + x[i+1]\n",
" sys.stdout.write(\"[%d] %d\\t\"%(i+1,y))\n",
"\n",
"sys.stdout.write(\"\\nEquivalent of [\")\n",
"\n",
"for i in range(0,5):\n",
" sys.stdout.write(\"%d\"%(x[i]))\n",
"\n",
"#Result\n",
"sys.stdout.write(\"] in Decimal Number is : \")\n",
"sys.stdout.write(\"%d\\t\"%(y))\n",
"\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Values in different Iterations\n",
"====== == ========= ==========\n",
"[1] 3\t[2] 7\t[3] 15\t[4] 31\t\n",
"Equivalent of [11111] in Decimal Number is : 31\t"
]
}
],
"prompt_number": 38
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.46, Page number: 144
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Program to add a parity bit with four binary bits such that the total\n",
"#number of one's should be even\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"bit = [0 for i in range(0,5)] #defines the array bit and initializes the elements with 0\n",
"c = 0\n",
"\n",
"sys.stdout.write(\"\\nEnter four bits : \")\n",
"\n",
"for j in range(0,4):\n",
" x = int(raw_input(\" \"))\n",
" bit[j] = x\n",
" if bit[j] == 1:\n",
" c += 1\n",
" else:\n",
" if bit[j] > 1 or bit[j] < 0:\n",
" j -= 1\n",
" continue\n",
" if c%2 == 0:\n",
" bit[j] = 0\n",
" else:\n",
" bit[j] = 1 \n",
" \n",
"#Result\n",
"sys.stdout.write(\"\\nMessage bits together with parity bit : \")\n",
"for j in range(0,5):\n",
" sys.stdout.write(\"%d\"%(bit[j]))\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter four bits : "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
" 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
" 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
" 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
" 1\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Message bits together with parity bit : 11110"
]
}
],
"prompt_number": 55
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.47, Page number: 145
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Convert binary to decimal number. Enter the binary bits by using for loop\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"\n",
"z = [0 for i in range(0,10)] #defines the array z and initializes the elements with 0\n",
"\n",
"sys.stdout.write(\"Enter the number of bits :- \")\n",
"b = int(raw_input(\"\"))\n",
"\n",
"sys.stdout.write(\"\\nEnter the binary bits : \")\n",
"for i in range(0,b):\n",
" z[i] = int(raw_input(\"\"))\n",
"\n",
"a = z[0]\n",
"\n",
"for i in range(0,b-1):\n",
" a = a * 2 + z[i+1]\n",
" sys.stdout.write(\"\\n%d\"%(a))\n",
" \n",
"#Result\n",
"sys.stdout.write(\"\\nDecimal Number is : %d \"%(a))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter the number of bits :- "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"5\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter the binary bits : "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"2\n",
"4\n",
"8\n",
"17\n",
"Decimal Number is : 17 "
]
}
],
"prompt_number": 56
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.48, Page number: 147
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Verify the truth table of AND gate. Assume AND gate has two\n",
"#input bits A & B and one output bit C.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"\n",
"a = [0 for i in range(0,4)] #defines the array a,b & c and initializes the elements with 0\n",
"b = [0 for i in range(0,4)]\n",
"c = [0 for i in range(0,4)]\n",
"\n",
"sys.stdout.write(\"\\nEnter Four Bits : \")\n",
"for x in range(0,4):\n",
" a[x] = int(raw_input(\"\"))\n",
" \n",
" if a[x] > 1 or a[x] < 0:\n",
" x -= 1\n",
" continue\n",
"\n",
"sys.stdout.write(\"\\nEnter Four Bits : \")\n",
"for x in range(0,4):\n",
" b[x] = int(raw_input(\"\"))\n",
" \n",
" if b[x] > 1 or b[x] < 0:\n",
" x -= 1\n",
" continue\n",
" \n",
"#Result\n",
"sys.stdout.write(\"\\nA B C\")\n",
"for x in range(0,4):\n",
" if a[x] == 1 and b[x] == 1:\n",
" c[x] = 1\n",
" else:\n",
" c[x] = 0\n",
" sys.stdout.write(\"\\n%d %d %d\"%(a[x],b[x],c[x]))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Four Bits : "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Four Bits : "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"A B C\n",
"1 1 1\n",
"0 0 0\n",
"1 0 0\n",
"0 1 0"
]
}
],
"prompt_number": 57
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.49, Page number: 148
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Verify the truth table of OR gate. \n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"\n",
"a = [0 for i in range(0,4)] #defines the array a,b & c and initializes the elements with 0\n",
"b = [0 for i in range(0,4)]\n",
"c = [0 for i in range(0,4)]\n",
"\n",
"sys.stdout.write(\"\\nEnter Four Bits : \")\n",
"for x in range(0,4):\n",
" a[x] = int(raw_input(\"\"))\n",
" \n",
" if a[x] > 1 or a[x] < 0:\n",
" x -= 1\n",
" continue\n",
"\n",
"sys.stdout.write(\"\\nEnter Four Bits : \")\n",
"for x in range(0,4):\n",
" b[x] = int(raw_input(\"\"))\n",
" \n",
" if b[x] > 1 or b[x] < 0:\n",
" x -= 1\n",
" continue\n",
" \n",
"#Result\n",
"sys.stdout.write(\"\\nA B C\")\n",
"for x in range(0,4):\n",
" if a[x] == 0 and b[x] == 0:\n",
" c[x] = 0\n",
" else:\n",
" c[x] = 1\n",
" sys.stdout.write(\"\\n%d %d %d\"%(a[x],b[x],c[x]))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Four Bits : "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Four Bits : "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"A B C\n",
"1 1 1\n",
"1 0 1\n",
"1 0 1\n",
"0 0 0"
]
}
],
"prompt_number": 58
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.50, Page number: 149
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Verify the truth table of EX-OR gate. \n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"\n",
"a = [0 for i in range(0,4)] #defines the array a,b & c and initializes the elements with 0\n",
"b = [0 for i in range(0,4)]\n",
"c = [0 for i in range(0,4)]\n",
"\n",
"sys.stdout.write(\"\\nEnter Four Bits : \")\n",
"for x in range(0,4):\n",
" a[x] = int(raw_input(\"\"))\n",
" \n",
" if a[x] > 1 or a[x] < 0:\n",
" x -= 1\n",
" continue\n",
"\n",
"sys.stdout.write(\"\\nEnter Four Bits : \")\n",
"for x in range(0,4):\n",
" b[x] = int(raw_input(\"\"))\n",
" \n",
" if b[x] > 1 or b[x] < 0:\n",
" x -= 1\n",
" continue\n",
" \n",
"#Result\n",
"sys.stdout.write(\"\\nA B C\")\n",
"for x in range(0,4):\n",
" if a[x] == 0 and b[x] == 1:\n",
" c[x] = 1\n",
" else:\n",
" if a[x] == 1 and b[x] == 0:\n",
" c[x] = 1\n",
" else:\n",
" c[x] = 0\n",
" sys.stdout.write(\"\\n%d %d %d\"%(a[x],b[x],c[x]))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Four Bits : "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Four Bits : "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"A B C\n",
"1 1 0\n",
"1 0 1\n",
"1 0 1\n",
"0 0 0"
]
}
],
"prompt_number": 59
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.51, Page number: 151
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Find the Hamming code for the entered binary code. Assume the binary code of four bits in length.\n",
"#The hamming code should be seven bits.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"\n",
"#defines the array a,b & c and initializes the elements with 0\n",
"b = [0 for i in range(0,4)]\n",
"c = [0 for i in range(0,7)]\n",
"\n",
"sys.stdout.write(\"\\nRead the Binary Numbers : \")\n",
"for x in range(0,4):\n",
" b[x] = int(raw_input(\"\"))\n",
"\n",
"#Piece copy operation\n",
"c[0] = b[0]\n",
"c[1] = b[1]\n",
"c[2] = b[2]\n",
"c[4] = b[3]\n",
"\n",
"sys.stdout.write(\"\\nBefore xOR operation : \")\n",
" \n",
"for x in range(0,7):\n",
" sys.stdout.write(\"%3d\"%(c[x]))\n",
"\n",
"c[6] = c[0] ^ c[2] ^ c[4]\n",
"c[5] = c[0] ^ c[1] ^ c[4]\n",
"c[3] = c[0] ^ c[1] ^ c[2]\n",
" \n",
"#Result\n",
"sys.stdout.write(\"\\nHamming code after XOR operation : \")\n",
"for x in range(0,7):\n",
" sys.stdout.write(\"%3d\"%(c[x]))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Read the Binary Numbers : "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"0\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Before xOR operation : 1 0 1 0 0 0 0\n",
"Hamming code after XOR operation : 1 0 1 0 0 1 0"
]
}
],
"prompt_number": 60
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.52, Page number: 152
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display the results of students appear in six subjects. The result declared should be as per the \n",
"#following table.\n",
"#TOTAL MARKS RESULT\n",
"#>= 420 Distinction\n",
"#>= 360 First Division\n",
"#>= 240 Second Division\n",
"#Otherwise Fail\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"DISTINCTION = 420\n",
"FIRST = 360\n",
"SECOND = 240\n",
"\n",
"number = int(raw_input(\"Enter number of Students : \"))\n",
"\n",
"#Read the values and calculation\n",
"for i in range(1,number+1):\n",
" roll_no = int(raw_input(\"Enter Roll Number : \"))\n",
" total = 0\n",
" sys.stdout.write(\"\\nEnter Marks of 6 Subjects for Roll no %d : \"%(roll_no))\n",
" for j in range(1,6+1):\n",
" marks = int(raw_input(\"\"))\n",
" total = total + marks\n",
" sys.stdout.write(\"\\nTOTAL MARKS = %d\"%(total))\n",
" if total >= DISTINCTION:\n",
" sys.stdout.write(\"\\n(Distinction)\\n\\n\")\n",
" else:\n",
" if total >= FIRST:\n",
" sys.stdout.write(\"\\n(First Division)\\n\\n\")\n",
" else:\n",
" if total >= SECOND:\n",
" sys.stdout.write(\"\\n(Second Division)\\n\\n\")\n",
" else:\n",
" sys.stdout.write(\"\\n(***Fail***)\")\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter number of Students : 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Roll Number : 1\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Marks of 6 Subjects for Roll no 1 : "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"42\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"52\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"62\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"72\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"82\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"92\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"TOTAL MARKS = 402\n",
"(First Division)\n",
"\n"
]
}
],
"prompt_number": 43
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.53, Page number: 154
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Print the string \"You have learnt C program\" 9 times using while loop\n",
"\n",
"#Variable Initialization\n",
"x = 1\n",
"\n",
"#Result\n",
"while x < 10:\n",
" sys.stdout.write(\"\\nYou have learnt C program\")\n",
" x += 1"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"You have learnt C program\n",
"You have learnt C program\n",
"You have learnt C program\n",
"You have learnt C program\n",
"You have learnt C program\n",
"You have learnt C program\n",
"You have learnt C program\n",
"You have learnt C program\n",
"You have learnt C program"
]
}
],
"prompt_number": 26
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.54, Page number: 155
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Add 10 consecutive numbers starting from 1 using while loop.\n",
"\n",
"#Variable Initialization\n",
"a = 1\n",
"sum1 = 0 #since sum is a builtin function in python, sum1 is used instead of sum\n",
"\n",
"#Calculation\n",
"while a <= 10:\n",
" sys.stdout.write(\"%3d\"%(a))\n",
" sum1 = sum1 + a\n",
" a += 1\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\nSum of 10 numbers : %d\"%(sum1))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
" 1 2 3 4 5 6 7 8 9 10\n",
"Sum of 10 numbers : 55"
]
}
],
"prompt_number": 27
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.55, Page number: 155
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Calculate factorial of a given number using while loop.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"fact = 1\n",
"\n",
"a = int(raw_input(\"Enter The Number : \"))\n",
"\n",
"#Calculation\n",
"while a >= 1:\n",
" sys.stdout.write(\" %d * \"%(a))\n",
" fact = fact * a\n",
" a -= 1\n",
"\n",
"#Result\n",
"sys.stdout.write(\" = %d\"%(fact))\n",
"sys.stdout.write(\"\\nFactorial of Given number is %d\"%(fact))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter The Number : 5\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" 5 * 4 * 3 * 2 * 1 * = 120\n",
"Factorial of Given number is 120"
]
}
],
"prompt_number": 61
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.56, Page number: 156
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Calculate factorial of a given number using while loop.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"fact = 1\n",
"b = 1\n",
"\n",
"a = int(raw_input(\"Enter The Number : \"))\n",
"\n",
"#Calculation\n",
"while b <= a:\n",
" sys.stdout.write(\" %d * \"%(b))\n",
" fact = fact * b\n",
" b += 1\n",
"\n",
"#Result\n",
"sys.stdout.write(\" = %d\"%(fact))\n",
"sys.stdout.write(\"\\nFactorial of %d is %d\"%(b-1,fact))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter The Number : 4\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" 1 * 2 * 3 * 4 * = 24\n",
"Factorial of 4 is 24"
]
}
],
"prompt_number": 62
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.57, Page number: 157
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Convert decimal number to binary number\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"y = 40\n",
"rev = [0 for i in range(1,10)]\n",
"c = 1\n",
"\n",
"x = int(raw_input(\"Enter a Number : \"))\n",
"\n",
"#Calculation & Result\n",
"#since gotoxy() function is not available in python, the reminders are stored in an array and displayed in reverse order\n",
"\n",
"sys.stdout.write(\"\\nBinary Number : \")\n",
"while x != 0:\n",
" rev[c] = x % 2\n",
" x = x / 2\n",
" c += 1\n",
"\n",
"for i in range(c-1,0,-1):\n",
" sys.stdout.write(\"%d\"%(rev[i]))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a Number : 25\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Binary Number : 11001"
]
}
],
"prompt_number": 63
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.58, Page number: 157
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Convert decimal number to user defined number system. \n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"y = 35\n",
"rev = 0\n",
"c = 1\n",
"rev = [0 for i in range(1,10)]\n",
"\n",
"m = int(raw_input(\"Enter the Decimal Number : \"))\n",
"b = int(raw_input(\"Enter base of number system : \"))\n",
"\n",
"#Calculation & Result\n",
"sys.stdout.write(\"\\nThe Number Obtained : \")\n",
"\n",
"#since gotoxy() function is not available in python, the reminders are stored in an array and displayed in reverse order\n",
"while m != 0:\n",
" rev[c] = m % b\n",
" c += 1\n",
" m = m / b\n",
"\n",
"for i in range(c-1,0,-1):\n",
" sys.stdout.write(\"%d\"%rev[i])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter the Decimal Number : 50\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter base of number system : 5\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"The Number Obtained : 200"
]
}
],
"prompt_number": 64
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.59, Page number: 158
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Convert binary number to equivalent decimal number\n",
"\n",
"\n",
"#Variable Initialization\n",
"y = 0\n",
"p = 0\n",
"\n",
"n = int(raw_input(\"Enter a Binary Number : \"))\n",
"\n",
"#Calculation\n",
"while n != 0:\n",
" x = n % 10\n",
" if x > 1 or x < 0:\n",
" sys.stdout.write(\"\\nInvalid Digit\")\n",
" break\n",
" else:\n",
" y = y + x * pow(2,p)\n",
" n = n / 10\n",
" p += 1\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\nEquivalent Decimal Number is %d.\"%(y))\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a Binary Number : 1111\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Equivalent Decimal Number is 15."
]
}
],
"prompt_number": 66
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.60, Page number: 159
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Read a positive integer number 'n' and generate the numbers in the following way.\n",
"#If entered number is 5 the output will be as follows.\n",
"#OUTPUT : 5 4 3 2 1 0 1 2 3 4 5\n",
"\n",
"\n",
"#Variable Initialization\n",
"k = 0\n",
"\n",
"n = int(raw_input(\"Enter a Number : \"))\n",
"\n",
"#Calculation & Result\n",
"i = n + 1\n",
"k = k - n\n",
"\n",
"while i != k:\n",
" sys.stdout.write(\"%3d\"%(abs(k)))\n",
" k += 1\n",
"\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a Number : 3\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" 3 2 1 0 1 2 3"
]
}
],
"prompt_number": 67
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.61, Page number: 160
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Enter a number through keyboard and find the sum of the digits.\n",
"\n",
"\n",
"#Variable Initialization\n",
"k = 1\n",
"sum1 = 0 #since sum is a built-in function in python, sum1 used instead of sum\n",
"\n",
"n = int(raw_input(\"Enter a Number : \"))\n",
"\n",
"#Calculation \n",
"while n != 0:\n",
" k = n % 10\n",
" sum1 = sum1 + k\n",
" k = n / 10\n",
" n = k\n",
"\n",
"#Result\n",
"sys.stdout.write(\"Sum of digits %d\"%(sum1))\n",
"\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a Number : 842\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Sum of digits 14"
]
}
],
"prompt_number": 68
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Find sum of odd and even numbers separately by sorting the given numbers into\n",
"#odd and even numbers\n",
"\n",
"#Variable Initialization\n",
"c = 1\n",
"odd = 0\n",
"even = 0\n",
"\n",
"a = int(raw_input(\"Enter a Number : \"))\n",
"\n",
"#Calculation & Result\n",
"sys.stdout.write(\"ODD EVEN\\n\")\n",
"while c <= a:\n",
" b = c % 2\n",
" while b == 0:\n",
" sys.stdout.write(\"\\t%d \"%(c))\n",
" even = even + c\n",
" b = 1\n",
" b = c % 2\n",
" while b != 0:\n",
" sys.stdout.write(\"\\n%d\"%(c))\n",
" odd = odd + c\n",
" b = 0\n",
" c += 1\n",
"\n",
"sys.stdout.write(\"\\n==============================\")\n",
"sys.stdout.write(\"\\n %d %d\"%(odd,even))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a Number : 10\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"ODD EVEN\n",
"\n",
"1\t2 \n",
"3\t4 \n",
"5\t6 \n",
"7\t8 \n",
"9\t10 \n",
"==============================\n",
" 25 30"
]
}
],
"prompt_number": 70
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.64, Page number: 163
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Print the entered number in reverse order.\n",
"\n",
"\n",
"#Variable Initialization\n",
"x = 1\n",
"\n",
"d = int(raw_input(\"Enter the number of digits : \"))\n",
"n = int(raw_input(\"Enter the number which is to be reversed : -\"))\n",
"\n",
"#Calculation & Result\n",
"sys.stdout.write(\"\\nThe Reversed Number is :-\")\n",
"\n",
"while x <= d:\n",
" i = n % 10\n",
" sys.stdout.write(\"%d\"%(i))\n",
" n = n / 10\n",
" x += 1"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter the number of digits : 4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter the number which is to be reversed : -5428\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"The Reversed Number is :-8245"
]
}
],
"prompt_number": 71
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.65, Page number: 163
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Separate capitals, small, symbols and numbers from a statement\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"i = 0\n",
"c = 0\n",
"s = 0\n",
"h = 0\n",
"n = 0\n",
"\n",
"#Array declaration\n",
"scan = [0 for i in range(0,40)]\n",
"cap = [0 for i in range(0,20)]\n",
"small = [0 for i in range(0,20)]\n",
"num = [0 for i in range(0,20)]\n",
"oth = [0 for i in range(0,20)]\n",
"\n",
"scan = raw_input(\"Enter Text Here : \")\n",
"\n",
"i = 0\n",
"\n",
"while i < len(scan): #There is no null termination character in python.\n",
" if scan[i] >= '0' and scan[i] <= '9':\n",
" n += 1\n",
" num[n] = scan[i]\n",
" else:\n",
" if scan[i] >= 'A' and scan[i] <= 'Z':\n",
" c += 1\n",
" cap[c] = scan[i]\n",
" else:\n",
" if scan[i] >= 'a' and scan[i] <= 'z':\n",
" s += 1\n",
" small[s] = scan[i]\n",
" else:\n",
" h += 1\n",
" oth[h] = scan[i]\n",
" \n",
" i += 1\n",
" \n",
"#Result\n",
"sys.stdout.write(\"\\nCapital Letters : [\")\n",
"for i in range(0,20):\n",
" sys.stdout.write(\"%c\"%cap[i])\n",
"\n",
"sys.stdout.write(\"]\\nSmall Letters : [\")\n",
"for i in range(0,20):\n",
" sys.stdout.write(\"%c\"%(small[i]))\n",
" \n",
"sys.stdout.write(\"]\\nNumeric Letters : [\")\n",
"for i in range(0,20):\n",
" sys.stdout.write(\"%c\"%(num[i]))\n",
"\n",
"sys.stdout.write(\"]\\nOther Letters : [\")\n",
"for i in range(0,20):\n",
" sys.stdout.write(\"%c\"%(oth[i]))\n",
"sys.stdout.write(\"]\")\n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Text Here : HAVE A NICE DAY, contact me on 51606.\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Capital Letters : [\u0000HAVEANICEDAY\u0000\u0000\u0000\u0000\u0000\u0000\u0000]\n",
"Small Letters : [\u0000contactmeon\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000]\n",
"Numeric Letters : [\u000051606\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000]\n",
"Other Letters : [\u0000 , .\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000]"
]
}
],
"prompt_number": 72
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.66, Page number: 165
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Sort numbers, upper and lower case alphabets\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"i = 48\n",
"\n",
"#Result\n",
"sys.stdout.write(\" NUMBERS\\n\")\n",
"while i <= 57:\n",
" sys.stdout.write(\"%2c\"%(i))\n",
" i += 1\n",
"\n",
"i += 7\n",
"\n",
"sys.stdout.write(\"\\n CAPITAL ALPHABETS\\n\")\n",
"while i <= 90:\n",
" sys.stdout.write(\"%2c\"%(i))\n",
" i += 1\n",
" \n",
"i += 6\n",
"\n",
"sys.stdout.write(\"\\n SMALL ALPHABETS\\n\")\n",
"while i <= 122:\n",
" sys.stdout.write(\"%2c\"%(i))\n",
" i += 1\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
" NUMBERS\n",
" 0 1 2 3 4 5 6 7 8 9\n",
" CAPITAL ALPHABETS\n",
" A B C D E F G H I J K L M N O P Q R S T U V W X Y Z\n",
" SMALL ALPHABETS\n",
" a b c d e f g h i j k l m n o p q r s t u v w x y z"
]
}
],
"prompt_number": 13
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.67, Page number: 165
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Sort numbers, upper and lower case alphabets\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"i = 48\n",
"\n",
"#Result\n",
"sys.stdout.write(\"Numbers : \")\n",
"while i < 124:\n",
" if i < 58:\n",
" sys.stdout.write(\"%2c\"%(i))\n",
" else:\n",
" if i == 58:\n",
" sys.stdout.write(\"\\nCapital Letters : \")\n",
" i += 7\n",
" if i > 64 and i < 91:\n",
" sys.stdout.write(\"%2c\"%(i))\n",
" if i == 90:\n",
" sys.stdout.write(\"\\nSmall Letters : \")\n",
" i += 7\n",
" if i > 96 and i < 123:\n",
" sys.stdout.write(\"%2c\"%(i))\n",
" i += 1"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Numbers : 0 1 2 3 4 5 6 7 8 9\n",
"Capital Letters : A B C D E F G H I J K L M N O P Q R S T U V W X Y Z\n",
"Small Letters : a b c d e f g h i j k l m n o p q r s t u v w x y z"
]
}
],
"prompt_number": 15
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Convert entered character into its opposite case. \n",
"\n",
"import sys\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\n*Enter 'E' to exit\")\n",
"sys.stdout.write(\"\\n*Enter only characters\")\n",
"\n",
"while c != 'E':\n",
" sys.stdout.write(\"\\nEnter a character : \")\n",
" c = raw_input(\"Enter a character :\")\n",
" if c == c.upper():\n",
" c = ord(c)\n",
" sys.stdout.write(\"\\tIt's Lower Case : %c\"%(chr(c+32)))\n",
" else:\n",
" c = ord(c)\n",
" sys.stdout.write(\"\\tIt's Upper Case : %s\"%(chr(c-32)))\n",
" c = chr(c)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"*Enter 'E' to exit\n",
"*Enter only characters\n",
"Enter a character : "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a character :s\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\tIt's Upper Case : S\n",
"Enter a character : "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a character :A\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\tIt's Lower Case : a\n",
"Enter a character : "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a character :E\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\tIt's Lower Case : e"
]
}
],
"prompt_number": 73
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.70, Page number: 169
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Print numbers after each iteration and messages after termination of each loop using 3 nested while loop.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"i = 1\n",
"j = 1\n",
"k = 1\n",
"\n",
"#Result\n",
"while i < 4:\n",
" while j < 3:\n",
" while k < 2:\n",
" sys.stdout.write(\"\\n\\ni = %d j = %d k = %d\"%(i,j,k))\n",
" k += 1\n",
" sys.stdout.write(\"\\nInner Loop (k) Completed.\")\n",
" k = 1\n",
" j += 1\n",
" sys.stdout.write(\"\\nMiddle Loop (j) Completed.\")\n",
" j = 1\n",
" i += 1\n",
"sys.stdout.write(\"\\nOuter Loop (i) Completed.\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
"i = 1 j = 1 k = 1\n",
"Inner Loop (k) Completed.\n",
"\n",
"i = 1 j = 2 k = 1\n",
"Inner Loop (k) Completed.\n",
"Middle Loop (j) Completed.\n",
"\n",
"i = 2 j = 1 k = 1\n",
"Inner Loop (k) Completed.\n",
"\n",
"i = 2 j = 2 k = 1\n",
"Inner Loop (k) Completed.\n",
"Middle Loop (j) Completed.\n",
"\n",
"i = 3 j = 1 k = 1\n",
"Inner Loop (k) Completed.\n",
"\n",
"i = 3 j = 2 k = 1\n",
"Inner Loop (k) Completed.\n",
"Middle Loop (j) Completed.\n",
"Outer Loop (i) Completed."
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.71, Page number: 170
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display a message \"This is a program of do while loop\". for 5 times using do while loop.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"i = 1\n",
"\n",
"#Result\n",
"#There is no do..while loop in python\n",
"while i <= 5:\n",
" sys.stdout.write(\"\\nThis is a program of do while loop.\")\n",
" i += 1\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"This is a program of do while loop.\n",
"This is a program of do while loop.\n",
"This is a program of do while loop.\n",
"This is a program of do while loop.\n",
"This is a program of do while loop."
]
}
],
"prompt_number": 13
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.72, Page number: 171
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Print the entered number in reverse order and find sum and product of digits using do-while loop. \n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"#since sum is a built-in function in python, sum1 is used instead of sum\n",
"x = 1\n",
"mul = 1\n",
"sum1 = 0\n",
"\n",
"d = int(raw_input(\"Enter the number of digits : -\"))\n",
"n = int(raw_input(\"Enter the number which is to be reversed :-\"))\n",
"\n",
"#Result\n",
"#There is no do..while loop in python\n",
"sys.stdout.write(\"\\nReveresed Number :- \")\n",
"while x <= d:\n",
" i = n % 10\n",
" sys.stdout.write(\"%d\"%(i))\n",
" sum1 = sum1 + i\n",
" mul = mul * i\n",
" n = n / 10\n",
" x += 1\n",
"\n",
"sys.stdout.write(\"\\nAddition of digits :- %4d\"%(sum1))\n",
"sys.stdout.write(\"\\nMultiplication of digits :- %4d\"%(mul))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter the number of digits : -4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter the number which is to be reversed :-4321\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Reveresed Number :- 1234\n",
"Addition of digits :- 10\n",
"Multiplication of digits :- 24"
]
}
],
"prompt_number": 74
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.73, Page number: 172
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Attempt the question 6.72 by considering the false condition i.e. the value of i should\n",
"#be initially larger than the value in the while loop.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"i = 7\n",
"\n",
"#Result\n",
"#There is no do-while loop in python\n",
"sys.stdout.write(\"\\nThis is a program of do while loop\")\n",
"while i <= 5:\n",
" sys.stdout.write(\"\\nThis is a program of do while loop\")\n",
" i += 1"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"This is a program of do while loop"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.74, Page number: 172
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Find the cubes of 1 to 10 numbers using do-while loop.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"x = 1\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\nPrint the numbers and their cubes\\n\")\n",
"while x <= 10:\n",
" y = pow(x,3)\n",
" sys.stdout.write(\"%4d %27d\\n\"%(x,y))\n",
" x += 1\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Print the numbers and their cubes\n",
" 1 1\n",
" 2 8\n",
" 3 27\n",
" 4 64\n",
" 5 125\n",
" 6 216\n",
" 7 343\n",
" 8 512\n",
" 9 729\n",
" 10 1000\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.75, Page number: 173
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Check whether the given number is prime or not.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"x = 2\n",
"n = int(raw_input(\"Enter The number for testing (prime or not) : \"))\n",
"\n",
"#Result\n",
"#There is no do-while loop in python\n",
"while x < n:\n",
" if n % x == 0:\n",
" sys.stdout.write(\"\\nThe number %d is not prime.\"%(n))\n",
" exit(0)\n",
" x += 1\n",
"\n",
"sys.stdout.write(\"\\nThe number %d is prime\"%(n))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter The number for testing (prime or not) : 5\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"The number 5 is prime"
]
}
],
"prompt_number": 75
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.76, Page number: 174
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Count the number of students having age less than 25 and weight less than 50Kg out of five.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"count = 0\n",
"x = 1\n",
"\n",
"sys.stdout.write(\"\\nEnter data of 5 boys\\n\")\n",
"sys.stdout.write(\"\\nAge Weight\\n\")\n",
"\n",
"#There is no do-while loop in python\n",
"while x <= 5:\n",
" age = int(raw_input(\"\"))\n",
" wt = float(raw_input(\"\"))\n",
" if age < 25 and wt < 50:\n",
" count += 1\n",
" x += 1\n",
" \n",
"#Result\n",
"sys.stdout.write(\"\\nNumber of boys with age < 25\")\n",
"sys.stdout.write(\"and weight < 50Kg = %d\\n\"%(count))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter data of 5 boys\n",
"\n",
"Age Weight\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"24\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"51\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"20\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"45\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"25\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"51\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"20\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"35\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"24\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"54\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Number of boys with age < 25and weight < 50Kg = 2\n"
]
}
],
"prompt_number": 76
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.77, Page number: 175
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Compute the factorial of given number using do while loop.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"fact = 1\n",
"a = int(raw_input(\"Enter The Number : \"))\n",
"sys.stdout.write(\"\\nEnter The Number : %d\\n\"%(a))\n",
"\n",
"#Calculation\n",
"#There is no do-while loop in python\n",
"while a >= 1:\n",
" sys.stdout.write(\"%d * \"%(a))\n",
" fact = fact * a\n",
" a -= 1\n",
" \n",
"#Result\n",
"sys.stdout.write(\" = %d\"%(fact))\n",
"sys.stdout.write(\"\\nFactorial of Given number is %d\"%(fact))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter The Number : 5\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter The Number : 5\n",
"5 * 4 * 3 * 2 * 1 * = 120\n",
"Factorial of Given number is 120"
]
}
],
"prompt_number": 77
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.78, Page number: 175
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Evaluate the series such as 1+2+3+ .. i. Using do-while loop. \n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"a = 1\n",
"s = 0\n",
"\n",
"i = int(raw_input(\"Enter a number : \"))\n",
"\n",
"#Calculation and Result\n",
"#There is no do-while loop in python\n",
"while a <= i:\n",
" sys.stdout.write(\"%d + \"%(a))\n",
" s = s + a\n",
" a += 1\n",
" \n",
"sys.stdout.write(\"\\b\\bs = %d\"%(s))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a number : 5\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1 + 2 + 3 + 4 + 5 + \b\bs = 15"
]
}
],
"prompt_number": 78
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.79, Page number: 176
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Use while statement in do-while loop and print values from 1 to 5\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"x = 0\n",
"\n",
"#There is no do-while loop in python\n",
"while x < 5:\n",
" x += 1\n",
" sys.stdout.write(\"\\t%d\"%(x))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\t1\t2\t3\t4\t5"
]
}
],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}