Wa0012
Wa0012
Wa0012
Question 1
Write a Python script that asks the user to enter a length in centimetres. 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 centimetres in an inch.
Solution
if len < 0:
print("Invalid input")
else:
Output
Question 2
A store charges ₹120 per item if you buy less than 10 items. If you buy between 10 and 99 items, the
cost is ₹100 per item. If you buy 100 or more items, the cost is ₹70 per item. Write a program that asks
the user how many items they are buying and prints the total cost.
Solution
cost = 0
if n >= 100 :
cost = n * 70
elif n >= 10 :
cost = n * 100
else :
cost = n * 120
Output
Question 3
Write a program that reads from user — (i) an hour between 1 to 12 and (ii) number of hours ahead.
The program should then print the time after those many hours, e.g.,
Solution
s = hr + n
if s > 12:
s -= 12
print("Time at that time would be : ", s, "O'clock")
Output
Question 4
Write a program that asks the user for two numbers and prints Close if the numbers are within .001 of
each other and Not close otherwise.
Solution
d=0
if a > b :
d=a-b
else :
d=b-a
if d <= 0.001 :
print("Close")
else :
print("Not Close")
Output
Question 5
A year is a leap year if it is divisible by 4, except that years divisible by 100 are not leap years unless they
are also divisible by 400. Write a program that asks the user for a year and prints out whether it is a leap
year or not.
Solution
if year % 400 == 0 :
elif year % 4 == 0 :
else :
Output
Question 6
Write a program to input length of three sides of a triangle. Then check if these sides will form a triangle
or not.
Solution
print("Triangle Possible")
else :
Output
Triangle Possible
Question 7
Solution
if d == 0 :
print("Zero")
elif d == 1 :
print("One")
elif d == 2 :
print("Two")
elif d == 3 :
print("Three")
elif d == 4 :
print("Four")
elif d == 5 :
print("Five")
elif d == 6 :
print("Six")
elif d == 7 :
print("Seven")
elif d == 8 :
print("Eight")
elif d == 9 :
print("Nine")
else :
print("Invalid Digit")
Output
Enter a digit(0-9): 6
Six
Question 8
Write a short program to check whether square root of a number is prime or not.
Solution
import math
c=0
if (sr % i == 0) :
c += 1
if c == 2 :
else :
Output
Enter a number: 49
Question 9
Solution
n = int(input("Enter n: "))
x=n*2-1
print(i)
Output
Enter n: 5
9
Question 10
Solution
print("First Series:")
print("\nSecond Series:")
x=1
x *= -1
Output
First Series:
1 4 7 10 13 16 19 22 25 28 31 34 37 40
Second Series:
1 -4 7 -10 13 -16 19 -22 25 -28 31 -34 37 -40
Question 11
Write a short program to find average of list of numbers entered through keyboard.
Solution
sum = count = 0
print("Enter numbers")
while True :
n = input()
if n == 'q' or n == 'Q' :
break
else :
sum += int(n)
count += 1
Output
Enter numbers
5
7
15
12
Average = 8.2
Question 12
Write a program to input 3 sides of a triangle and print whether it is an equilateral, scalene or isosceles
triangle.
Solution
if a == b and b == c :
print("Equilateral Triangle")
elif a == b or b == c or c == a:
print("Isosceles Triangle")
else :
print("Scalene Triangle")
Output
Isosceles Triangle
Question 13
Write a program to take an integer a as an input and check whether it ends with 4 or 8. If it ends with 4,
print "ends with 4", if it ends with 8, print "ends with 8", otherwise print "ends with neither".
Solution
if a % 10 == 4 :
elif a % 10 == 8 :
else :
Output
Enter an integer: 18
ends with 8
Question 14
Write a program to take N (N > 20) as an input from the user. Print numbers from 11 to N. When the
number is a multiple of 3, print "Tipsy", when it is a multiple of 7, print "Topsy". When it is a multiple of
both, print "TipsyTopsy".
Solution
if n <= 20 :
print("Invalid Input")
else :
for i in range(11, n + 1) :
print(i)
if i % 3 == 0 and i % 7 == 0 :
print("TipsyTopsy")
elif i % 3 == 0 :
print("Tipsy")
elif i % 7 == 0 :
print("Topsy")
Output
11
12
Tipsy
13
14
Topsy
15
Tipsy
16
17
18
Tipsy
19
20
21
TipsyTopsy
22
23
24
Tipsy
25
Question 15
Write a short program to find largest number of a list of numbers entered through keyboard.
Solution
print("Enter numbers:")
l = input()
l = int(l)
while True:
n = input()
if n == 'q' or n == 'Q' :
break
n = int(n)
if n > l :
l=n
Output
Enter numbers:
(Enter 'q' to see the result)
Largest Number = 8
Question 16
Write a program to input N numbers and then print the second largest number.
Solution
if n > 1 :
if sl > l :
t = sl
sl = l
l=t
for i in range(n - 2) :
a = int(input())
if a > l :
sl = l
l=a
elif a > sl :
sl = a
else :
Output
55
25
36
12
18
Question 17
Given a list of integers, write a program to find those which are palindromes. For example, the number
4321234 is a palindrome as it reads the same from left to right and from right to left.
Solution
print("Enter numbers:")
while True :
n = input()
if n == 'q' or n == 'Q' :
break
n = int(n)
t=n
r=0
while (t != 0) :
d = t % 10
r = r * 10 + d
t = t // 10
if (n == r) :
else :
Output
Enter numbers:
67826
4321234
256894
122221
Question 18
(iii) form an integer Y that has the number of digits n at ten's place and the most significant digit of X at
one's place.
(iv) Output Y.
(For example, if X is equal to 2134, then Y should be 42 as there are 4 digits and the most significant
number is 2).
Solution
temp = x
count = 0
digit = -1
while temp != 0 :
digit = temp % 10
count += 1
temp = temp // 10
y = count * 10 + digit
print("Y =", y)
Output
Y = 42
Question 19
Write a Python program to print every integer between 1 and n divisible by m. Also report whether the
number that is divisible by m is even or odd.
Solution
m = int(input("Enter m: "))
n = int(input("Enter n: "))
for i in range(1, n) :
if i % m == 0 :
if i % 2 == 0 :
else :
Output
Enter m: 3
Enter n: 20
3 is divisible by 3
3 is odd
6 is divisible by 3
6 is even
9 is divisible by 3
9 is odd
12 is divisible by 3
12 is even
15 is divisible by 3
15 is odd
18 is divisible by 3
18 is even
Question 20a
Solution
sum = 0
for i in range(7) :
t=n/d
sum += t * m
n += 3
d += 4
m *= -1
Sum = 0.3642392586003134
Question 20b
12 + 32 + 52 + ..... + n2 (Input n)
Solution
i=1
sum = 0
while i <= n :
sum += i ** 2
i += 2
Output
Sum = 165
Question 21
sum = 0
for i in range(n + 1) :
fact = 1
for j in range(1, i) :
fact *= j
term = 1 / fact
sum += term
Output
Sum = 3.708333333333333
Question 22
Write a program to accept the age of n employees and count the number of persons in the following age
group:
(i) 26 - 35
(ii) 36 - 45
(iii) 46 - 55
Solution
g1 = g2 = g3 = 0
for i in range(1, n + 1) :
g1 += 1
g2 += 1
g3 += 1
Output
Question 23a
Solution
sum = 0
m=1
for i in range(1, 7) :
fact = 1
fact *= j
term = x ** i / fact
sum += term * m
m = m * -1
Output
Enter the value of x: 2
Sum = 0.8444444444444444
Question 23b
Solution
sum = 0
for i in range(1, n + 1) :
term = x ** i / i
sum += term
Output
Sum = 17.066666666666666
Question 24a
**
***
**
Solution
n = 3 # number of rows
# upper half
for i in range(n) :
for k in range(i+1) :
print()
# lower half
for i in range(n-1) :
for j in range(i + 1) :
print()
Output
*
**
***
**
Question 24b
**
***
**
Solution
n = 3 # number of rows
# upper half
for i in range(n) :
for k in range(i+1) :
print()
# lower half
for i in range(n-1) :
for k in range(n-1, i, -1) :
print()
Output
**
***
**
Question 24c
* *
* *
* *
Solution
n = 3 # number of rows
# upper half
x=1
while x < 2 * i :
if x == 1 or x == 2 * i - 1 :
else :
x += 1
print()
# lower half
x=1
while x < 2 * i :
if x == 1 or x == 2 * i - 1 :
else :
print()
Output
**
* *
**
Question 24d
**
* *
* *
* *
**
Solution
n = 4 # number of row
#upper half
while x < 2 * i :
if x == 1 or x == 2 * i - 1 :
else :
x += 1
print()
#lower half
x=1
while x < 2 * i :
if x == 1 or x == 2 * i - 1 :
else :
x += 1
print()
Output
**
* *
* *
* *
**
Question 25a
AB
ABC
ABCD
ABCDE
ABCDEF
Solution
n=6
for i in range(n) :
t = 65
for j in range(i + 1) :
t += 1
print()
Output
AB
ABC
ABCD
ABCDE
ABCDEF
Question 25b
BB
CCC
DDDD
EEEEE
Solution
n=5
t = 65
for i in range(n) :
for j in range(i + 1) :
t += 1
print()
Output
BB
CCC
DDDD
EEEEE
Question 25c
22
444
6666
88888
Solution
for j in range(0, i + 1, 2) :
print()
Output
22
444
6666
88888
Question 25d
44
666
8888
Solution
for j in range(2, i + 1, 2) :
print()
Output
44
666
8888
Question 26
Write a program using nested loops to produce a rectangle of *'s with 6 rows and 20 *'s per row.
Solution
for i in range(6) :
for j in range(20) :
print()
Output
********************
********************
********************
********************
********************
********************
Question 27
Given three numbers A, B and C, write a program to write their values in an ascending order. For
example, if A = 12, B = 10, and C = 15, your program should print out:
Smallest number = 10
Highest number = 15
Solution
small = a
if b < c :
middle = b
large = c
else :
middle = c
large = b
small = b
if a < c :
middle = a
large = c
else :
middle = c
large = a
else :
small = c
if a < b :
middle = a
large = b
else :
middle = b
large = a
Output
Smallest number = 5
Highest number = 15
Question 28
Write a Python script to input temperature. Then ask them what units, Celsius or Fahrenheit, the
temperature is in. Your program should convert the temperature to the other unit. The conversions are:
Solution
newTemp = 9 / 5 * temp + 32
else :
Output
Enter Temperature: 38
Ask the user to enter a temperature in Celsius. The program should print a message based on the
temperature:
If the temperature is less than -273.15, print that the temperature is invalid because it is below absolute
zero.
If the temperature is between -273.15 and 0, print that the temperature is below freezing.
If it is between 0 and 100, print that the temperature is in the normal range.
If it is above 100, print that the temperature is above the boiling point.
Solution
elif temp == 0 :
else :
Output
Question 30
Write a program to display all of the integers from 1 up to and including some integer entered by the
user followed by a list of each number's prime factors. Numbers greater than 1 that only have a single
prime factor will be marked as prime.
For example, if the user enters 10 then the output of the program should be:
1=1
2 = 2 (prime)
3= 3 (prime)
4 = 2x2x
5 = 5 (prime)
6 = 2x3x
7 = 7 (prime)
8 = 2x2x2x
9 = 3x3x
10 = 2x5x