#Print the first five numbers starting from one together with thier squares
#Result
#Since range() function starts with 0; to print upto 5, use 5+1 or 6
#range() function creates range or numbers from 1 to 6
for i in range(1,5+1):
print("Number : %d it's Square : %d"%(i,i*i))
#Display numbers from 1 to 15 using for loop. Use i++
import sys
#Result
#There is no increment operator (++) in python
#the range() function creates range of numbers and for loop automatically
#iterate through the numbers
for i in range(1,15+1):
sys.stdout.write("%5d"%(i))
#Display numbers from 1 to 15 using for loop. Use i = i + 1
import sys
#Result
#the range() function creates range of numbers and for loop automatically
#iterate through the numbers
for i in range(1,15+1):
sys.stdout.write("%5d"%(i))
#Display numbers from 1 to 16. Use incrementation operation in the body of
#the loop for more than once
import sys
#Variable declaration
c = 0
#Result
#the range() function creates range of numbers and for loop automatically
#iterate through the numbers and the third argument in the range function
#indicates the interval between the numbers.
for i in range(0,15+1,2):
i += 1
sys.stdout.write("%5d"%(i))
i = i + 1
sys.stdout.write("%5d"%(i))
c += 1
sys.stdout.write("\n\nThe Body of the loop is executed for %d times"%(c))
#Display even numbers from 0 to 14 by declaring the initial counter value before
#the loop statement
import sys
#Variable declaration
c = 0
i = 0
#Result
#the range() function creates range of numbers and for loop automatically
#iterate through the numbers and the third argument in the range function
#indicates the interval between the numbers.
#counter variable initialization before the for loop will not affect much.
for i in range(0,15+1,2):
sys.stdout.write("%5d"%(i))
#Display numbers in ascending and descending orders.
import sys
#Variable declaration
i = 0
#Result
#the range() function creates range of numbers and for loop automatically
#iterate through the numbers and the third argument in the range function
#indicates the interval between the numbers.
#counter variable initialization before the for loop will not affect much.
sys.stdout.write("Numbers in Ascending Order : ")
for i in range(1,10+1):
sys.stdout.write("%3d"%(i))
sys.stdout.write("\nNumbers in Descending Order : ")
for i in range(10,0,-1):
sys.stdout.write("%3d"%(i))
#Display numbers upto 10 using infinite for loop. Use goto statement to
#exit from the loop
import sys
#Variable declaration
i = 0
#Result
#the range() function creates range of numbers and for loop automatically
#iterate through the numbers and the third argument in the range function
#indicates the interval between the numbers.
#counter variable initialization before the for loop will not affect much.
for i in range(0,15):
sys.stdout.write("%3d"%(i))
i += 1
if i == 11:
break
#there is no infinite for loop and goto statement in python.
#Count numbers between 1 to 100 not divisible by 2, 3, and 5
import sys
#Variable declaration
c = 0
sys.stdout.write("\nNumbers from 1 to 100 not divisible by 2,3&5\n\n")
#Result
#the range() function creates range of numbers and for loop automatically
#iterate through the numbers
for x in range(0,100+1):
if x%2 != 0 and x%3 != 0 and x%5 != 0:
sys.stdout.write("%d\t"%(x))
c += 1
sys.stdout.write("\nTotal Numbers : %d"%(c))
#Display the numbers in increasing and decreasing order using infinite
#for loop
import sys
#Variable declaration
n = int(raw_input("Enter a number : "))
a = b = n
sys.stdout.write("(++) (--)\n")
sys.stdout.write("===================")
#Result
#the range() function creates range of numbers and for loop automatically
#iterate through the numbers
#There is no infinite for loop in python. so separate iteration variable i is used
for i in range(0,100):
sys.stdout.write("\n%d\t%d"%(a,b))
if b == 0:
break
a += 1
b -= 1
#Create an infinite loop. Check each value of the for loop. if the value is
#even, display it otherwise continue with interations. Print even numbers from
#1 to 21. Use break statement to terminate the program.
import sys
#Variable declaration
i = 1
sys.stdout.write("\n\t\tTable of Even numbers from 1 to 20")
sys.stdout.write("\n\t\t==================================\n")
#Result
#the range() function creates range of numbers and for loop automatically
#iterate through the numbers
#There is no infinite for loop in python. so separate iteration variable i is used
for i in range(1,100):
if i == 20:
break
else:
if i%2 == 0:
sys.stdout.write("%d\t"%(i))
continue
else:
continue
#Calculate the sum of first five numbers and their squares. Display their results
import sys
#Variable declaration
#since sum is a function in python, sum1 is used instead of variable sum.
sum1 = 0
sqsum = 0
#Result
#the range() function creates range of numbers and for loop automatically
#iterate through the numbers
for i in range(1,5+1):
sum1 += i
sqsum += i*i
sys.stdout.write("\nNumber : %5d it's Square : %8d"%(i,i*i))
sys.stdout.write("\n=================================================")
sys.stdout.write("\nThe Sum %6d Sum of Squares %9d"%(sum1,sqsum))
#Display numbers from 1 to 9 and their square roots.
import math
import sys
#Result
#the range() function creates range of numbers and for loop automatically
#iterate through the numbers
for i in range(1,9+1):
a = math.sqrt(i)
sys.stdout.write("\n\t%d %.2f"%(i,a))
#Find the number in between 7 and 100 which is exactly divisible by
#4 and if divided by 5 and 6 remainders obtained should be 4.
import sys
#Result
#the range() function creates range of numbers and for loop automatically
#iterate through the numbers
for x in range(7,100+1):
if x%4 == 0 and x%5 == 4 and x%6 == 4:
sys.stdout.write("\nMinimum Number : %d"%(x))
#Evaluate the series given
# x = 1/1 + 1/4 + 1/9 ... 1/n2
# y = 1/1 + 1/8 + 1/27 ... 1/n3
#Variable declaration
x = 0.0
y = 0.0
n = int(raw_input("Enter Value of n : "))
#Result
#the range() function creates range of numbers and for loop automatically
#iterate through the numbers
for i in range(1,n+1):
x = x + (1.0/pow(i,2))
y = y + (1.0/pow(i,3))
print("Value of x = %f"%(x))
print("\nValue of y = %f"%(y))
#Generate Triangular number
#Note: Triangular number is nothing but summation of 1 to given number.
#For example, when entered number is 5 it's triangular number would be
#(1+2+3+4+5) = 15.
#Variable declaration
tri_num = 0
n = int(raw_input("What Triangular number do you want : "))
#Result
#the range() function creates range of numbers and for loop automatically
#iterate through the numbers
for j in range(1,n+1):
tri_num = tri_num + j
print("Triangular number of %d is %d\n"%(n,tri_num))
#Find sum of the following series
# 1 + 2 + 3 + ... n
# 1^2 + 2^2 + 2^3 + ... n^2
import sys
#Variable declaration
#since sum is a function name in python, sum1 is used instead of sum.
sum1 = 0
ssum = 0
j = int(raw_input("Enter Number : "))
#Result
#the range() function creates range of numbers and for loop automatically
#iterate through the numbers
sys.stdout.write("Numbers : ")
for i in range(1,j+1):
sys.stdout.write("%5d"%(i))
sys.stdout.write("\nSquares : ")
for i in range(1,j+1):
sys.stdout.write("%5d"%(i*i))
sum1 = sum1 + i
ssum = ssum + i*i
sys.stdout.write("\nSum of Numbers from 1 to %d : %d"%(j,sum1))
sys.stdout.write("\nSum of Squares of 1 to %d Numbers : %d"%(j,ssum))
#Display the perfect squares from 1 to 500.
import sys
import math
#Result
#the range() function creates range of numbers and for loop automatically
#iterate through the numbers
sys.stdout.write("\n\n")
sys.stdout.write("Perfect square from 1 to 500 \n")
count = 0
for i in range(1,500+1):
c = math.sqrt(i)
x = math.floor(c) #For rounding up floor() is used
if c == x:
sys.stdout.write("\t%5d"%(i))
count += 1
sys.stdout.write("\n\nTotal Perfect Squares = %d"%(count))
#Detect the largest number out of five numbers.
import sys
import math
#Result
#the range() function creates range of numbers and for loop automatically
#iterate through the numbers.
#since sum is a built in function in python, sum1 is used instead of sum.
a = int(raw_input("Enter five numbers : "))
b = int(raw_input("Enter five numbers : "))
c = int(raw_input("Enter five numbers : "))
d = int(raw_input("Enter five numbers : "))
e = int(raw_input("Enter five numbers : "))
sum1 = a + b + c + d + e
for i in range(sum1,1,-1):
if i == a or i == b or i == c or i == d or i == e:
sys.stdout.write("The Largest Number : %d"%(i))
break
#Detect the smallesst number out of five numbers.
import sys
import math
#Result
#the range() function creates range of numbers and for loop automatically
#iterate through the numbers.
#since sum is a built in function in python, sum1 is used instead of sum.
a = int(raw_input("Enter five numbers : "))
b = int(raw_input("Enter five numbers : "))
c = int(raw_input("Enter five numbers : "))
d = int(raw_input("Enter five numbers : "))
e = int(raw_input("Enter five numbers : "))
sum1 = a + b + c + d + e
for i in range(1,sum1):
if i == a or i == b or i == c or i == d or i == e:
sys.stdout.write("The Smallest Number : %d"%(i))
break
#Read five numbers and print in ascending order
import sys
import math
#Result
#the range() function creates range of numbers and for loop automatically
#iterate through the numbers.
#since sum is a built in function in python, sum1 is used instead of sum.
#use commas to separate the input values
a = int(raw_input("Enter five numbers : "))
b = int(raw_input("Enter five numbers : "))
c = int(raw_input("Enter five numbers : "))
d = int(raw_input("Enter five numbers : "))
e = int(raw_input("Enter five numbers : "))
sys.stdout.write("Numbers in ascending order : ")
sum1 = a + b + c + d + e
for i in range(1,sum1):
if i == a or i == b or i == c or i == d or i == e:
sys.stdout.write("%3d"%(i))
#Perform multiplication of two integers by using negative sign
import sys
#Result
#the range() function creates range of numbers and for loop automatically
#iterate through the numbers.
a = int(raw_input("Enter two numbers : "))
b = int(raw_input("Enter two numbers : "))
d = 0
for c in range(1,b+1):
d = d - (-a)
sys.stdout.write("Multiplication of %d * %d : %d"%(a,b,d))
#Perform multiplication of two integers by using repetitive addition
import sys
#Result
#in python, infinite for loop is not possible.
a = int(raw_input("Enter two numbers : "))
b = int(raw_input("Enter two numbers : "))
d = 0
c = 1
while True:
d = d + a
if c == b:
break
c += 1
sys.stdout.write("Multiplication of %d * %d : %d"%(a,b,d))
#Read the marks for five subjects and calculate sum & average of marks.
import sys
#Result
#use spaces to separate the input values
#since sum is a built-in function in python, sum1 is used as the variable name
sys.stdout.write("Enter The Marks of Five Subjects ")
sum1 = 0
for i in range(1,5+1):
sys.stdout.write("[%d] Student : "%(i))
#Exception handling
try:
#use space to separate the input characters
v = raw_input("")
a,b,c,d,e = map(int,v.split())
x = 5
except:
x = 0
if(x == 5):
sum1 = a + b + c + d + e
avg = sum1/5
sys.stdout.write("\nTotal Marks of Student [%d] %d"%(i,sum1))
sys.stdout.write("\nAverage Marks of Student [%d] %f"%(i,avg))
else:
sys.stdout.write("Type Mismatch")
#Enter a character and display its position in alphabetic order.
import sys
#Variable Initialization
c = 1
ch = raw_input("Enter a character :")
ch = ch.upper()
#Result
#ord() function converts the character ASCII value to integer
for i in range(65,90+1):
if ord(ch) == i:
sys.stdout.write("'%c' is %d [st/nd/rd/th] Character in Alphabetic"%(i,c))
c += 1
#Calculate the value of -m^x.
#'m' is a mantissa and 'x' is an exponent
import sys
#Variable Initialization
number = int(raw_input("Enter Number : "))
power = int(raw_input("Enter Power : "))
ans = 1
#Calculation
for i in range(1,power+1):
ans *= number
#Result
sys.stdout.write("The Power of %d raised to %d is %ld"%(number,power,ans))
#Subtraction of two loop variables using nested for loops.
import sys
#Calculation & Result
for a in range(3,0,-1):
for b in range(1,2+1):
sub = a - b
sys.stdout.write("a = %d b = %d a - b = %d\n"%(a,b,sub))
#Illustrate an example based on nested for loops
import sys
#Calculation & Result
for i in range(1,3+1): #outer loop
for j in range(1,2+1): #inner loop
sys.stdout.write("\ni * j : %d"%(i*j))
#Print values and messages when any loop ends using nested for loops.
import sys
#Calculation & Result
for a in range(1,2+1): #outer loop
for b in range(1,2+1): #middle loop
for c in range(1,2+1): #inner loop
sys.stdout.write("\na = %d + b = %d + c = %d : %d"%(a,b,c,a+b+c))
sys.stdout.write("\nInner Loop Over.")
sys.stdout.write("\nMiddle Loop Over.")
sys.stdout.write("\nOuter Loop Over.")
#Display perfect cubes up to given number.
import sys
#Variable Initialization
k = int(raw_input("Enter a Number : "))
#Calculation & Result
for i in range(1,k+1):
for j in range(1,i+1):
if i == pow(j,3):
sys.stdout.write("\nNumber : %d & it's Cube : %d"%(j,i))
#Display numbers 1 to 100 using ASCII values from 48 to 57. Use nested loops.
import sys
#Variable Initialization
j = 0
k = -9
sys.stdout.write("\tTable of 1 to 100 Numbers Using ASCII Values\n")
sys.stdout.write("\t===== == = == === ======= ===== ===== ======\n")
#Calculation & Result
for i in range(48,57+1):
for j in range(48,57+1):
sys.stdout.write("\t%c%c"%(i,j))
#if k != 1:
# sys.stdout.write("%c"%(i+1))
#if k == 0:
# sys.stdout.write("\b\b%d"%(k+1))
sys.stdout.write("\n")
k += 1
#Count number of votes secured by 'A' & 'B'. Assume three voters are
#voting them. Also count the invalid votes.
import sys
#Variable Initialization & Calculation
a = 0
b = 0
o = 0
sys.stdout.write("Press A or B\n")
for i in range(1,3+1):
sys.stdout.write("\nVoter no. %d"%(i))
sys.stdout.write(" Enter Vote : ")
v = raw_input("")
v = v.upper()
if v == 'A':
a += 1
else:
if v == 'B':
b += 1
else:
o += 1
#Result
sys.stdout.write("\nStatus of vote(s)\n")
sys.stdout.write("\nA secures %d vote(s)."%(a))
sys.stdout.write("\nB secures %d vote(s)."%(b))
sys.stdout.write("\nInvalid vote(s) %d."%(o))
#Simulate a digital clock
import sys
import os
#Result
sys.stdout.write("hh mm ss\n")
for h in range(1,12):
for m in range(1,59):
for s in range(1,59):
sys.stdout.write("\r%d %d %d"%(h,m,s))
#Displays the last value
#Count occurrence of 0 to 9 digits between 1 and given decimal number
import sys
#Variable Initialization
n = int(raw_input("Enter a Decimal Number : "))
st = [0 for x in range(10)] #10 array elements initialized as 0
#Calculation
for l in range(1,n+1):
t = l
while t != 0:
k = t % 10
t = t / 10
st[k] += 1
#Result
sys.stdout.write("\nOccurrence of 0-9 digits between 1 to %d Numbers."%(n))
sys.stdout.write("\n========== == === ====== ======= = == == ========")
for i in range(0,10):
if st[i] > 0:
sys.stdout.write("\n%d Occurs %8d Times."%(i,st[i]))
#Display sum of digits of a given number
import sys
#Variable Initialization
s = 0
n = int(raw_input("Enter a Number : "))
#Calculation & Result
sys.stdout.write("\nSum of Digits till a single digit\n %d"%(n))
while n != 0:
s = s + n%10
n = n/10
if n == 0 and s > 9:
sys.stdout.write("\n %2d"%(s))
n = s
s = 0
sys.stdout.write("\n %2d"%(s))
#Display octal numbers in binary. Attach a parity bit with "1"
#if number of 1s are even otherwise "0".
# OR
#Generate odd parity to octal numbers 0 to 7. Express each number in binary
#and attach the parity bit
import sys
#Variable Initialization
c = 0
j = 12
k = 2
#Calculation & Result
sys.stdout.write("\nBinary Bits Parity Bits")
sys.stdout.write("\n============= ==============\n")
for x in range(0,8):
k += 1
j = 12
y = x
for a in range(0,3):
b = y % 2
#gotoxy
sys.stdout.write("\t%d"%(b))
y = y / 2
if b == 1:
c += 1
if c%2 == 0:
#gotoxy
sys.stdout.write("\t1")
else:
#gotosy
sys.stdout.write("\t0\n")
c = 0
#Evaluate the series x - x^3/3! + x^5/5! - .... x^n/n!
import sys
#Variable Initialization
c = 3.0
f = 1.0
#Give x as float value and n as int value
x = float(raw_input("Enter x & n : ")) #use commas to input values
n = int(raw_input("Enter x & n : "))
sum1 = x #since sum is built-in function in python,
#sum1 is used instead of sum
#Calculation & Result
for i in range(3,n+1,2):
f = 1
if c%2 != 0:
for l in range(1,i+1):
f = f * l
sum1 = sum1 - pow(x,i)/ f
else:
for l in range(1,i+1):
f = f * l
sum1 = sum1 + pow(x,i)/ f
c += 1
sys.stdout.write("\nSum of series Numbers : %f"%(sum1))
#Evaluate the series x + x^2/2! + x^4/4! + .... x^n/n!
import sys
#Variable Initialization
f = 1.0
#Give x as float value and y as int value
x = float(raw_input("Enter x & y : ")) #use commas to input values
y = int(raw_input("Enter x & y : "))
sum1 = x #since sum is built-in function in python,
#sum1 is used instead of sum
#Calculation & Result
for i in range(2,y+1,2):
f = 1
for l in range(1,i+1):
f = f * l
sum1 = sum1 + pow(x,i)/ f
sys.stdout.write("\nSum of Series : %f"%(sum1))
#Evaluate the series 1 - 1/1! + 2/2! - 3/3! .... n/n!
import sys
#Variable Initialization
c = 3
n = int(raw_input("Enter value of n : "))
sum1 = 1.0 #since sum is built-in function in python,
#sum1 is used instead of sum
#Calculation
for i in range(1,n+1):
f = 1.0
if c%2 != 0:
for l in range(1,i+1):
f = f * l
k = float(i / f)
sum1 = sum1 - k
else:
for l in range(1,i+1):
f = f * l
sum1 = sum1 + float(i/f)
c += 1
#Result
sys.stdout.write("\nSum of series Numbers : %f"%(sum1))
#Display the Armstrong numbers in three digits from 100 to 999. If sum of cubes of each digits
#of the number is equal to number itself, then the number is called as an Armstrong number.
#(For eg. 153 = 1^3 + 5^3 + 3^3 = 153)
import sys
#Variable Initialization
cube = 0
sys.stdout.write("The Following Numbers are Armstrong numbers.")
#Calculation & Result
for k in range(100,999+1):
cube = 0
x = 1
d = 3
n = k
while x <= d:
i = n % 10
cube = cube + pow(i,3)
n = n / 10
x += 1
if cube == k:
sys.stdout.write("\n\t%d"%(k))
#Example 6.40.py
#Program to display the stars as shown below
#*
#**
#***
#****
#*****
import sys
#Variable Initialization
x = int(raw_input("How many lines stars (*) should be printed ? "))
#Result
for i in range(1,x+1):
for j in range(1,i+1):
sys.stdout.write("*")
sys.stdout.write("\n")
#Generate the given pattern
#6 5 4 3 2 1 0
#5 4 3 2 1 0
#4 3 2 1 0
#3 2 1 0
#2 1 0
#1 0
#0
import sys
#Variable Initialization
i = int(raw_input("Enter a Number : "))
#Result
while i >= 0:
c = i
sys.stdout.write("\n")
while True:
sys.stdout.write("%3d"%(c))
if c == 0:
break
c -= 1
i -= 1
#Display the series of numbers as given below
#1
#1 2
#1 2 3
#1 2 3 4
#4 3 2 1
#3 2 1
#2 1
#1
import sys
#Variable Initialization
x = int(raw_input("Enter value of x : "))
#Result
for j in range(1,x+1):
for i in range(1,j+1):
sys.stdout.write("%3d"%(i))
sys.stdout.write("\n")
sys.stdout.write("\n")
for j in range(x,0,-1):
for i in range(j,0,-1):
sys.stdout.write("%3d"%(i))
sys.stdout.write("\n")
#Program to display the series of numbers as given below
#1
#2 1
#3 2 1
#4 3 2 1
#4 3 2 1
#3 2 1
#2 1
#1
import sys
#Variable Initialization
x = int(raw_input("Enter value of x : "))
#Result
for j in range(1,x+1):
for i in range(j,0,-1):
sys.stdout.write("%3d"%(i))
sys.stdout.write("\n")
sys.stdout.write("\n")
for j in range(x,0,-1):
for i in range(j,0,-1):
sys.stdout.write("%3d"%(i))
sys.stdout.write("\n")
#Generate the pyramid structure using numberical
import sys
#Variable Initialization
x = int(raw_input("Enter a number : "))
#Result
for j in range(0,x+1):
for i in range(0-j,j+1):
sys.stdout.write("%3d"%(abs(i)))
sys.stdout.write("\n")
#Convert binary to decimal number
import sys
#Variable Initialization
x = [1 for i in range(1,5+1)] #defines the array x and initializes the elements with 0
y = x[0]
sys.stdout.write("\nValues in different Iterations")
sys.stdout.write("\n====== == ========= ==========\n")
for i in range(0,4):
y = y * 2 + x[i+1]
sys.stdout.write("[%d] %d\t"%(i+1,y))
sys.stdout.write("\nEquivalent of [")
for i in range(0,5):
sys.stdout.write("%d"%(x[i]))
#Result
sys.stdout.write("] in Decimal Number is : ")
sys.stdout.write("%d\t"%(y))
#Program to add a parity bit with four binary bits such that the total
#number of one's should be even
import sys
#Variable Initialization
bit = [0 for i in range(0,5)] #defines the array bit and initializes the elements with 0
c = 0
sys.stdout.write("\nEnter four bits : ")
for j in range(0,4):
x = int(raw_input(" "))
bit[j] = x
if bit[j] == 1:
c += 1
else:
if bit[j] > 1 or bit[j] < 0:
j -= 1
continue
if c%2 == 0:
bit[j] = 0
else:
bit[j] = 1
#Result
sys.stdout.write("\nMessage bits together with parity bit : ")
for j in range(0,5):
sys.stdout.write("%d"%(bit[j]))
#Convert binary to decimal number. Enter the binary bits by using for loop
import sys
#Variable Initialization
z = [0 for i in range(0,10)] #defines the array z and initializes the elements with 0
sys.stdout.write("Enter the number of bits :- ")
b = int(raw_input(""))
sys.stdout.write("\nEnter the binary bits : ")
for i in range(0,b):
z[i] = int(raw_input(""))
a = z[0]
for i in range(0,b-1):
a = a * 2 + z[i+1]
sys.stdout.write("\n%d"%(a))
#Result
sys.stdout.write("\nDecimal Number is : %d "%(a))
#Verify the truth table of AND gate. Assume AND gate has two
#input bits A & B and one output bit C.
import sys
#Variable Initialization
a = [0 for i in range(0,4)] #defines the array a,b & c and initializes the elements with 0
b = [0 for i in range(0,4)]
c = [0 for i in range(0,4)]
sys.stdout.write("\nEnter Four Bits : ")
for x in range(0,4):
a[x] = int(raw_input(""))
if a[x] > 1 or a[x] < 0:
x -= 1
continue
sys.stdout.write("\nEnter Four Bits : ")
for x in range(0,4):
b[x] = int(raw_input(""))
if b[x] > 1 or b[x] < 0:
x -= 1
continue
#Result
sys.stdout.write("\nA B C")
for x in range(0,4):
if a[x] == 1 and b[x] == 1:
c[x] = 1
else:
c[x] = 0
sys.stdout.write("\n%d %d %d"%(a[x],b[x],c[x]))
#Verify the truth table of OR gate.
import sys
#Variable Initialization
a = [0 for i in range(0,4)] #defines the array a,b & c and initializes the elements with 0
b = [0 for i in range(0,4)]
c = [0 for i in range(0,4)]
sys.stdout.write("\nEnter Four Bits : ")
for x in range(0,4):
a[x] = int(raw_input(""))
if a[x] > 1 or a[x] < 0:
x -= 1
continue
sys.stdout.write("\nEnter Four Bits : ")
for x in range(0,4):
b[x] = int(raw_input(""))
if b[x] > 1 or b[x] < 0:
x -= 1
continue
#Result
sys.stdout.write("\nA B C")
for x in range(0,4):
if a[x] == 0 and b[x] == 0:
c[x] = 0
else:
c[x] = 1
sys.stdout.write("\n%d %d %d"%(a[x],b[x],c[x]))
#Verify the truth table of EX-OR gate.
import sys
#Variable Initialization
a = [0 for i in range(0,4)] #defines the array a,b & c and initializes the elements with 0
b = [0 for i in range(0,4)]
c = [0 for i in range(0,4)]
sys.stdout.write("\nEnter Four Bits : ")
for x in range(0,4):
a[x] = int(raw_input(""))
if a[x] > 1 or a[x] < 0:
x -= 1
continue
sys.stdout.write("\nEnter Four Bits : ")
for x in range(0,4):
b[x] = int(raw_input(""))
if b[x] > 1 or b[x] < 0:
x -= 1
continue
#Result
sys.stdout.write("\nA B C")
for x in range(0,4):
if a[x] == 0 and b[x] == 1:
c[x] = 1
else:
if a[x] == 1 and b[x] == 0:
c[x] = 1
else:
c[x] = 0
sys.stdout.write("\n%d %d %d"%(a[x],b[x],c[x]))
#Find the Hamming code for the entered binary code. Assume the binary code of four bits in length.
#The hamming code should be seven bits.
import sys
#Variable Initialization
#defines the array a,b & c and initializes the elements with 0
b = [0 for i in range(0,4)]
c = [0 for i in range(0,7)]
sys.stdout.write("\nRead the Binary Numbers : ")
for x in range(0,4):
b[x] = int(raw_input(""))
#Piece copy operation
c[0] = b[0]
c[1] = b[1]
c[2] = b[2]
c[4] = b[3]
sys.stdout.write("\nBefore xOR operation : ")
for x in range(0,7):
sys.stdout.write("%3d"%(c[x]))
c[6] = c[0] ^ c[2] ^ c[4]
c[5] = c[0] ^ c[1] ^ c[4]
c[3] = c[0] ^ c[1] ^ c[2]
#Result
sys.stdout.write("\nHamming code after XOR operation : ")
for x in range(0,7):
sys.stdout.write("%3d"%(c[x]))
#Display the results of students appear in six subjects. The result declared should be as per the
#following table.
#TOTAL MARKS RESULT
#>= 420 Distinction
#>= 360 First Division
#>= 240 Second Division
#Otherwise Fail
import sys
#Variable Initialization
DISTINCTION = 420
FIRST = 360
SECOND = 240
number = int(raw_input("Enter number of Students : "))
#Read the values and calculation
for i in range(1,number+1):
roll_no = int(raw_input("Enter Roll Number : "))
total = 0
sys.stdout.write("\nEnter Marks of 6 Subjects for Roll no %d : "%(roll_no))
for j in range(1,6+1):
marks = int(raw_input(""))
total = total + marks
sys.stdout.write("\nTOTAL MARKS = %d"%(total))
if total >= DISTINCTION:
sys.stdout.write("\n(Distinction)\n\n")
else:
if total >= FIRST:
sys.stdout.write("\n(First Division)\n\n")
else:
if total >= SECOND:
sys.stdout.write("\n(Second Division)\n\n")
else:
sys.stdout.write("\n(***Fail***)")
#Print the string "You have learnt C program" 9 times using while loop
#Variable Initialization
x = 1
#Result
while x < 10:
sys.stdout.write("\nYou have learnt C program")
x += 1
#Add 10 consecutive numbers starting from 1 using while loop.
#Variable Initialization
a = 1
sum1 = 0 #since sum is a builtin function in python, sum1 is used instead of sum
#Calculation
while a <= 10:
sys.stdout.write("%3d"%(a))
sum1 = sum1 + a
a += 1
#Result
sys.stdout.write("\nSum of 10 numbers : %d"%(sum1))
#Calculate factorial of a given number using while loop.
import sys
#Variable Initialization
fact = 1
a = int(raw_input("Enter The Number : "))
#Calculation
while a >= 1:
sys.stdout.write(" %d * "%(a))
fact = fact * a
a -= 1
#Result
sys.stdout.write(" = %d"%(fact))
sys.stdout.write("\nFactorial of Given number is %d"%(fact))
#Calculate factorial of a given number using while loop.
import sys
#Variable Initialization
fact = 1
b = 1
a = int(raw_input("Enter The Number : "))
#Calculation
while b <= a:
sys.stdout.write(" %d * "%(b))
fact = fact * b
b += 1
#Result
sys.stdout.write(" = %d"%(fact))
sys.stdout.write("\nFactorial of %d is %d"%(b-1,fact))
#Convert decimal number to binary number
import sys
#Variable Initialization
y = 40
rev = [0 for i in range(1,10)]
c = 1
x = int(raw_input("Enter a Number : "))
#Calculation & Result
#since gotoxy() function is not available in python, the reminders are stored in an array and displayed in reverse order
sys.stdout.write("\nBinary Number : ")
while x != 0:
rev[c] = x % 2
x = x / 2
c += 1
for i in range(c-1,0,-1):
sys.stdout.write("%d"%(rev[i]))
#Convert decimal number to user defined number system.
import sys
#Variable Initialization
y = 35
rev = 0
c = 1
rev = [0 for i in range(1,10)]
m = int(raw_input("Enter the Decimal Number : "))
b = int(raw_input("Enter base of number system : "))
#Calculation & Result
sys.stdout.write("\nThe Number Obtained : ")
#since gotoxy() function is not available in python, the reminders are stored in an array and displayed in reverse order
while m != 0:
rev[c] = m % b
c += 1
m = m / b
for i in range(c-1,0,-1):
sys.stdout.write("%d"%rev[i])
#Convert binary number to equivalent decimal number
#Variable Initialization
y = 0
p = 0
n = int(raw_input("Enter a Binary Number : "))
#Calculation
while n != 0:
x = n % 10
if x > 1 or x < 0:
sys.stdout.write("\nInvalid Digit")
break
else:
y = y + x * pow(2,p)
n = n / 10
p += 1
#Result
sys.stdout.write("\nEquivalent Decimal Number is %d."%(y))
#Read a positive integer number 'n' and generate the numbers in the following way.
#If entered number is 5 the output will be as follows.
#OUTPUT : 5 4 3 2 1 0 1 2 3 4 5
#Variable Initialization
k = 0
n = int(raw_input("Enter a Number : "))
#Calculation & Result
i = n + 1
k = k - n
while i != k:
sys.stdout.write("%3d"%(abs(k)))
k += 1
#Enter a number through keyboard and find the sum of the digits.
#Variable Initialization
k = 1
sum1 = 0 #since sum is a built-in function in python, sum1 used instead of sum
n = int(raw_input("Enter a Number : "))
#Calculation
while n != 0:
k = n % 10
sum1 = sum1 + k
k = n / 10
n = k
#Result
sys.stdout.write("Sum of digits %d"%(sum1))
#Enter few numbers and count the positive and negative numbers together
#with their sums. When 0 is entered program should be terminated.
#Variable Initialization
p = 0
n = 0
j = 1
s = 0
ns = 0
#Calculation
sys.stdout.write("Enter Numbers (0) Exit : ")
while j != 0:
j = int(raw_input(""))
if j > 0:
p += 1
s = s + j
else:
if j < 0:
n += 1
ns = ns + j
#Result
sys.stdout.write("\nTotal Positive Numbers : %d"%(p))
sys.stdout.write("\nTotal Negative Numbers : %d"%(n))
sys.stdout.write("\nSum of Positive Numbers : %d"%(s))
sys.stdout.write("\nSum of Negative Numbers : %d"%(ns))
#Find sum of odd and even numbers separately by sorting the given numbers into
#odd and even numbers
#Variable Initialization
c = 1
odd = 0
even = 0
a = int(raw_input("Enter a Number : "))
#Calculation & Result
sys.stdout.write("ODD EVEN\n")
while c <= a:
b = c % 2
while b == 0:
sys.stdout.write("\t%d "%(c))
even = even + c
b = 1
b = c % 2
while b != 0:
sys.stdout.write("\n%d"%(c))
odd = odd + c
b = 0
c += 1
sys.stdout.write("\n==============================")
sys.stdout.write("\n %d %d"%(odd,even))
#Print the entered number in reverse order.
#Variable Initialization
x = 1
d = int(raw_input("Enter the number of digits : "))
n = int(raw_input("Enter the number which is to be reversed : -"))
#Calculation & Result
sys.stdout.write("\nThe Reversed Number is :-")
while x <= d:
i = n % 10
sys.stdout.write("%d"%(i))
n = n / 10
x += 1
#Separate capitals, small, symbols and numbers from a statement
import sys
#Variable Initialization
i = 0
c = 0
s = 0
h = 0
n = 0
#Array declaration
scan = [0 for i in range(0,40)]
cap = [0 for i in range(0,20)]
small = [0 for i in range(0,20)]
num = [0 for i in range(0,20)]
oth = [0 for i in range(0,20)]
scan = raw_input("Enter Text Here : ")
i = 0
while i < len(scan): #There is no null termination character in python.
if scan[i] >= '0' and scan[i] <= '9':
n += 1
num[n] = scan[i]
else:
if scan[i] >= 'A' and scan[i] <= 'Z':
c += 1
cap[c] = scan[i]
else:
if scan[i] >= 'a' and scan[i] <= 'z':
s += 1
small[s] = scan[i]
else:
h += 1
oth[h] = scan[i]
i += 1
#Result
sys.stdout.write("\nCapital Letters : [")
for i in range(0,20):
sys.stdout.write("%c"%cap[i])
sys.stdout.write("]\nSmall Letters : [")
for i in range(0,20):
sys.stdout.write("%c"%(small[i]))
sys.stdout.write("]\nNumeric Letters : [")
for i in range(0,20):
sys.stdout.write("%c"%(num[i]))
sys.stdout.write("]\nOther Letters : [")
for i in range(0,20):
sys.stdout.write("%c"%(oth[i]))
sys.stdout.write("]")
#Sort numbers, upper and lower case alphabets
import sys
#Variable Initialization
i = 48
#Result
sys.stdout.write(" NUMBERS\n")
while i <= 57:
sys.stdout.write("%2c"%(i))
i += 1
i += 7
sys.stdout.write("\n CAPITAL ALPHABETS\n")
while i <= 90:
sys.stdout.write("%2c"%(i))
i += 1
i += 6
sys.stdout.write("\n SMALL ALPHABETS\n")
while i <= 122:
sys.stdout.write("%2c"%(i))
i += 1
#Sort numbers, upper and lower case alphabets
import sys
#Variable Initialization
i = 48
#Result
sys.stdout.write("Numbers : ")
while i < 124:
if i < 58:
sys.stdout.write("%2c"%(i))
else:
if i == 58:
sys.stdout.write("\nCapital Letters : ")
i += 7
if i > 64 and i < 91:
sys.stdout.write("%2c"%(i))
if i == 90:
sys.stdout.write("\nSmall Letters : ")
i += 7
if i > 96 and i < 123:
sys.stdout.write("%2c"%(i))
i += 1
#Display all ASCII numbers and their equivalent character, numbers and symbols using while loop.
import sys
#Variable Initialization
i = 64
c = 'Y'
#Result
while c == 'Y':
sys.stdout.write("\t%c (%d)"%(i,i))
i += 1
sys.stdout.write("\nPrint (Y/N)")
c = raw_input("")
#Convert entered character into its opposite case.
import sys
#Result
sys.stdout.write("\n*Enter 'E' to exit")
sys.stdout.write("\n*Enter only characters")
while c != 'E':
sys.stdout.write("\nEnter a character : ")
c = raw_input("Enter a character :")
if c == c.upper():
c = ord(c)
sys.stdout.write("\tIt's Lower Case : %c"%(chr(c+32)))
else:
c = ord(c)
sys.stdout.write("\tIt's Upper Case : %s"%(chr(c-32)))
c = chr(c)
#Print numbers after each iteration and messages after termination of each loop using 3 nested while loop.
import sys
#Variable Initialization
i = 1
j = 1
k = 1
#Result
while i < 4:
while j < 3:
while k < 2:
sys.stdout.write("\n\ni = %d j = %d k = %d"%(i,j,k))
k += 1
sys.stdout.write("\nInner Loop (k) Completed.")
k = 1
j += 1
sys.stdout.write("\nMiddle Loop (j) Completed.")
j = 1
i += 1
sys.stdout.write("\nOuter Loop (i) Completed.")
#Display a message "This is a program of do while loop". for 5 times using do while loop.
import sys
#Variable Initialization
i = 1
#Result
#There is no do..while loop in python
while i <= 5:
sys.stdout.write("\nThis is a program of do while loop.")
i += 1
#Print the entered number in reverse order and find sum and product of digits using do-while loop.
import sys
#Variable Initialization
#since sum is a built-in function in python, sum1 is used instead of sum
x = 1
mul = 1
sum1 = 0
d = int(raw_input("Enter the number of digits : -"))
n = int(raw_input("Enter the number which is to be reversed :-"))
#Result
#There is no do..while loop in python
sys.stdout.write("\nReveresed Number :- ")
while x <= d:
i = n % 10
sys.stdout.write("%d"%(i))
sum1 = sum1 + i
mul = mul * i
n = n / 10
x += 1
sys.stdout.write("\nAddition of digits :- %4d"%(sum1))
sys.stdout.write("\nMultiplication of digits :- %4d"%(mul))
#Attempt the question 6.72 by considering the false condition i.e. the value of i should
#be initially larger than the value in the while loop.
import sys
#Variable Initialization
i = 7
#Result
#There is no do-while loop in python
sys.stdout.write("\nThis is a program of do while loop")
while i <= 5:
sys.stdout.write("\nThis is a program of do while loop")
i += 1
#Find the cubes of 1 to 10 numbers using do-while loop.
import sys
#Variable Initialization
x = 1
#Result
sys.stdout.write("\nPrint the numbers and their cubes\n")
while x <= 10:
y = pow(x,3)
sys.stdout.write("%4d %27d\n"%(x,y))
x += 1
#Check whether the given number is prime or not.
import sys
#Variable Initialization
x = 2
n = int(raw_input("Enter The number for testing (prime or not) : "))
#Result
#There is no do-while loop in python
while x < n:
if n % x == 0:
sys.stdout.write("\nThe number %d is not prime."%(n))
exit(0)
x += 1
sys.stdout.write("\nThe number %d is prime"%(n))
#Count the number of students having age less than 25 and weight less than 50Kg out of five.
import sys
#Variable Initialization
count = 0
x = 1
sys.stdout.write("\nEnter data of 5 boys\n")
sys.stdout.write("\nAge Weight\n")
#There is no do-while loop in python
while x <= 5:
age = int(raw_input(""))
wt = float(raw_input(""))
if age < 25 and wt < 50:
count += 1
x += 1
#Result
sys.stdout.write("\nNumber of boys with age < 25")
sys.stdout.write("and weight < 50Kg = %d\n"%(count))
#Compute the factorial of given number using do while loop.
import sys
#Variable Initialization
fact = 1
a = int(raw_input("Enter The Number : "))
sys.stdout.write("\nEnter The Number : %d\n"%(a))
#Calculation
#There is no do-while loop in python
while a >= 1:
sys.stdout.write("%d * "%(a))
fact = fact * a
a -= 1
#Result
sys.stdout.write(" = %d"%(fact))
sys.stdout.write("\nFactorial of Given number is %d"%(fact))
#Evaluate the series such as 1+2+3+ .. i. Using do-while loop.
import sys
#Variable Initialization
a = 1
s = 0
i = int(raw_input("Enter a number : "))
#Calculation and Result
#There is no do-while loop in python
while a <= i:
sys.stdout.write("%d + "%(a))
s = s + a
a += 1
sys.stdout.write("\b\bs = %d"%(s))
#Use while statement in do-while loop and print values from 1 to 5
import sys
#Variable Initialization
x = 0
#There is no do-while loop in python
while x < 5:
x += 1
sys.stdout.write("\t%d"%(x))