Flow of Control - Type C Answers
Flow of Control - Type C Answers
1. Write a python script that ask the user to enter a length in centimeters. If the user enters a
negative length, the program should tell the user that the entry is invalid. otherwise, The
program should convert the length to inches and print out the result. there are 2.54
centimeters in an inch.
Code:
n = int(input("Enter the length in centimeter = "))
if n < 0 :
print("Invalid")
else :
length=n/2.54
print("Length in inch = ", length)
2. A store charges RS 120 per item if you buy less than 10 items. If you buy between 10 and
99 items ,the cost is RS100 per item . if you buy more items, The cost is RS 70 the user how
many items they are buying and print the total cost.
Code:
item = int(input("Enter the total number of items = "))
if item < 10 :
tot_cost=item * 120
print("Total cost = ",tot_cost , "rs")
elif 10 <= item and item <= 99 :
tot_cost=item * 100
print("Total cost = ",tot_cost, "rs")
else :
tot_cost=item * 70
print("Total cost = ",tot_cost , "rs")
3. Write a program to input length of three side of a triangle. then check if these side will
form a triangle or not.
Code:
a = int (input ("Enter the first side of triangle = "))
b = int (input ("Enter the second side of triangle = "))
c = int (input ("Enter the third side of triangle = "))
if a < b + c and b < a + c and c < a + b :
print ("It form the triangle")
else :
print ("It can not form triangle")
5. Write a program to take two numbers and print if the first number is fully divisible by
second number or not.
Code:
m = int(input("Enter the number = "))
n = int(input("Enter the divisor = "))
if m%n == 0:
print("It is divisible ")
else:
print("It is not divisible")
6. Write a short program to input a digit(1 -10) and print it in words.
Code:
day_no = int (input ("Enter the digit (1-10) ="))
if day_no ==1 :
print ("one ")
elif day_no ==2 :
print ("two")
elif day_no ==3 :
print ("three")
elif day_no == 4:
print ("four ")
elif day_no == 5:
print ("five")
elif day_no == 6:
print ("six ")
elif day_no == 7:
print ("seven")
elif day_no == 8:
print ("eight ")
elif day_no == 9:
print ("nine")
elif day_no == 10 :
print ("ten")
else:
print ("digit is out of range ")
7. Write a short program to print first n odd number in descending.
Code:
n = int(input("Enter value of n = "))
for i in range( (n * 2 - 1), 0 , -2) :
print(i, end = " , ")
8. Write a short program to print the following series:
(i) 1 4 7 10 ………. 40
(ii) 1 -4 7 -10 ………….. -40
Code:
(i)
for i in range(1,41,3):
print(i,end=" , ")
(ii)
for i in range(1,41,3):
if i % 2 == 0 :
print(-i,end=" , ")
else :
print(i,end=" , ")
9. Write a short program to find average of list of number entered through keyboard.
Code:
sum = 0
feq = 0
while True:
a = input("Enter a number (for quit enter Yes )= ")
if a == "Yes" or a == "yes" :
print("average of numbers = ",sum/feq)
break
else:
sum = sum + int(a)
feq = feq + 1
10. Write a short program to find largest number of the list of number entered through
keyboard.
Code:
b=0
while True :
a = int(input("Enter a number (for quit enter 0 (zero) )= "))
if a == 0 or a == 0 :
break
else :
if a > b :
b=a
print(b, "is biggest number.")
11. Write a program to input assets, liabilities and capital of a company and test if accounting
equation holds true for the given value (i.e., balanced or not).
Code:
assets = float(input("Enter assets :-"))
liabilities = float(input("Enter liabilities :-"))
capital = float(input("Enter capital :-"))