Flow of Control Book programs
Program 1 = Program that takes a number and checks whether the
given number is odd or even.
INPUT:
num = int ( input( "Enter an integer : " ) )
if num % 2 :
print (num, "is EVEN number." )
else :
print (num, "is ODD number.")
Program 2 = Program to accept three integers and print the largest
of the three. Make use of only if statement.
INPUT:
x = float (input( "Enter first number ) )
y = float (input( "Enter second number : " ) )
z = float (input( "Enter third number ) )
max = x
if y > max :
max = y
if z > max :
max = z
print ("Largest number is", max)
Program 3 = Program that inputs three numbers and calculates two
sums per this :
Sum 1 = Sum of all input numbers
Sum 2 = as the sum of non - duplicate numbers ; if there are
duplicate numbers in the input, ignore them
INPUT:
sum1 = sum2 = 0
numl = int(input("Enter number 1 : “)
num2 = int(input("Enter number 2 : “))
num3 = int(input("Enter number 3 " ) )
suml = numl + num2 + num3
if numl ! = num2 and numl ! = num3 :
sum 2 += num1
if num2 ! = numl and num2 ! = num3
sum2 += num2
if num3 != numl and num3 != num2
sum2 += num3
print ("Numbers are",numl, num2, num3)
print( "Sum of three given numbers is" , suml)
print("Sum of non-duplicate numbers is" , sum2)
Program 4 = Program to test the divisibilty of a number with another
number.
INPUT:
number1 = int(input(“Enter the first number : ”))
number2 = int(input(“Enter the second number : ”))
remainder = number1 % number2
if remainder == 0 :
print(number1,”is divisible by “,number 2 )
else :
print(number1,”is not divisible by “,number 2 )
Program 5 = Program to find the multiples of a number out of given
five numbers.
INPUT:
print("Enter five numbers below : ")
num1 = float(input("First number = "))
num2 = float(input("Second number = "))
num3 = float(input("Third number = "))
num4 = float(input("Fourth number = "))
num5 = float(input("Fifth number = "))
divisor = float(input("Enter divisor number = "))
count = 0
print("Multiples of ",divisor,"are = ")
remainder = num1 % divisor
if remainder == 0 :
print(num1,sep =" ")
count +=1
remainder = num2 % divisor
if remainder == 0 :
print(num2,sep =" ")
count +=1
remainder = num3 % divisor
if remainder == 0 :
print(num3,sep =" ")
count +=1
remainder = num4 % divisor
if remainder == 0 :
print(num4,sep =" ")
count +=1
remainder = num5 % divisor
if remainder == 0 :
print(num5,sep =" ")
count +=1
print()
print(count,"multiples of ",divisor,"found")
Program 6 = Program to display a menu for calculating area of a
circle or perimeter of a circle.
INPUT:
radius = float(input("Enter radius of a circle = "))
print("1.Calculate Area = ")
print("2.Calculate Perimeter = ")
choice=int (input( "Enter your choice (1 or 2) :"))
if choice==1:
area = 3.14159 * radius * radius
print ("Area of circle with radius",radius, 'is', area)
else :
perm = 2 * 3.14159 * radius
print ("Perimeter of circle with radius", radius, 'is', perm)1
Program 7 = Program that reads two numbers and an arthimetic
operator and displays the computed result.
INPUT:
num1 = float(input("Enter first number = "))
num2 = float(input("Enter second number = "))
op = input("Enter operator [ + - * / % ] :")
result = 0
if op == '+':
result = num1 + num2
elif op == '-':
result = num1 - num2
elif op == '*':
result = num1 * num2
elif op == '/':
result = num1 / num2
elif op == '%':
result = num1 % num2
else :
print ("Invalid operator! ! ")
print (num1, op, num2,'=' , result)
Program 8 = Program that reads three numbers and prints them in
ascending order.
INPUT:
x = int (input( "Enter first number :”))
y = int (input( "Enter second number :”))
z = int (input( "Enter third number :”))
min = mid= max = None
if x < y and x < z:
if y < z:
min, mid, max = x,y,z
else :
min, mid, max = x, z, y
elif y< x and y < z :
if x < z:
min, mid, max = y, x, z
else :
min, mid, max = y, z, x
else :
if x < y:
min, mid, max = z, x, y
else:
min, mid, max = z, y, x
Program 9 = Program to print whether a given character is an
uppercase or a lowercase character or a digit or any other character.
INPUT:
ch = input( "Enter a single character : “)
if ch >= 'A' and ch 'Z':
print ("You entered an Upper case character.”)
elif ch >= 'a' and ch <= 'z':
print ("You entered a lower case character. ")
elif ch >= '0 ' and ch '9':
print ("You entered a digit." )
else :
print ("You entered a special character. ")
Program 10 = Program to calculate and print the roots of the
quadratic equation ax2+bx+c=0.
INPUT:
import math
print(" For quadratic equation, ax**2+bx+c=0,enter coefficients below - ")
a = int(input("Enter a : "))
b = int(input("Enter b : "))
c = int(input("Enter c : "))
print("The Quadratic Equation is = ",a,'x^2 + ',b,'x + ',c,' = 0 ')
if a == 0:
print ("Value of",a, 'should not be zero')
print("\ n Aborting !!!!!!!")
else:
delta = b*b-4*a*c
if delta > 0 :
root1 = (-b+math.sqrt(delta))/(2*a)
root2 = (-b+math.sqrt(delta))/(2*a)
print("Roots are REAL and UNEQUAL")
print("Root 1 = ",root1,"Root 2 = ",root2)
elif delta == 0 :
root1 = -b/(2*a)
print("Roots are REAL and EQUAL")
print("Root 1 = ",root1,"Root 2 = ",root1)
else:
print("Roots are COMPLEX and IMAGINARY")
Program 11 = Program to print table of a number,say 5.
INPUT:
num = 5
for a in range (1,11):
print(num,’x’,a ‘=’ , num *a)
Program 12 = Program to print sum o/ natural numbers between I to
7. Pont the sum progressively. i.e. after adding each natural number,
print sum so far.
INPUT:
sum = 0
for n in range(1, 8) :
sum += n
print ("Sum of natural numbers <=",n, "is", sum)
Program 13 = Program to print sum of natural numbers between 1 to
7.
INPUT:
sum = 0
for n in range(1, 8):
sum += n
print ("Sum of natural numbers n <=, 'is', sum)
Program 14 = Program to calculate factorial of a number
INPUT:
num = int(input(“Enter a number : “))
fact = 1
a =1
while a < = num :
fact *=a
a +=1
print ("The factorial of" , num, "is" , fact)
Program 15 =Program to calculate and print the sums of even and odd
integers of the first n natural numbers.
INPUT:
n = int(input( "Up to which natural number ) )
ctr = 1
sum_even = sum_odd = 0
while ctr <= n :
if ctr%2== 0 :
sum_even += ctr
else :
sum odd ctr
print ("The sum of even integers is" , sum_even)
print ("The sum of odd integers is" sum_odd
Program 16 =Program to implement ‘guess the number’ game. Python
genenrates a number randimy in the range [10,50]. the user is given
5 chances to guess a number in the range 10 <= 50
INPUT:
import random
number = random. randint(10, 50)
ctr = 0
while ctr < 5 :
guess = int(input( "Guess a number in range 10...........50 = "))
if guess == number :
print ("You win !!!")
break
else:
ctr+=1
if not ctr< 5 :
print("You lost : (\n the number was",number)