Wa0012

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 39

Type C: Programming Practice/Knowledge based Questions

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

len = int(input("Enter length in cm: "))

if len < 0:

print("Invalid input")

else:

inch = len / 2.54

print(len, "centimetres is equal to", inch, "inches")

Output

Enter length in cm: 150

150 centimetres is equal to 59.05511811023622 inches

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

n = int(input("Enter number of items: "))

cost = 0

if n >= 100 :
cost = n * 70

elif n >= 10 :

cost = n * 100

else :

cost = n * 120

print("Total Cost =", cost)

Output

Enter number of items: 58

Total Cost = 5800

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.,

Enter hour between 1-12 : 9

How many hours ahead : 4

Time at that time would be : 1 O'clock

Solution

hr = int(input("Enter hour between 1-12 : "))

n = int(input("How many hours ahead : "))

s = hr + n

if s > 12:

s -= 12
print("Time at that time would be : ", s, "O'clock")

Output

Enter hour between 1-12 : 9

How many hours ahead : 4

Time at that time would be : 1 O'clock

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

a = float(input("Enter first number: "))

b = float(input("Enter second number: "))

d=0

if a > b :

d=a-b

else :

d=b-a

if d <= 0.001 :

print("Close")

else :

print("Not Close")

Output

Enter first number: 10.12345

Enter second number: 10.12354


Close

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

year = int(input("Enter year: "))

if year % 400 == 0 :

print(year, "is a Leap Year")

elif year % 100 == 0 :

print(year, "is not a Leap Year")

elif year % 4 == 0 :

print(year, "is a Leap Year")

else :

print(year, "is not a Leap Year")

Output

Enter year: 1800

1800 is not a Leap Year

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.

(Rule is: a+b>c;b+c>a;c+a>b)

Solution

a = int(input("Enter first side : "))


b = int(input("Enter second side : "))

c = int(input("Enter third side : "))

if a + b > c and b + c > a and a + c > b :

print("Triangle Possible")

else :

print("Triangle Not Possible")

Output

Enter first side : 3

Enter second side : 5

Enter third side : 6

Triangle Possible

Question 7

Write a short program to input a digit and print it in words.

Solution

d = int(input("Enter a digit(0-9): "))

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

n = int(input("Enter a number: "))


sr = math.sqrt(n)

c=0

for i in range(1, int(sr + 1)) :

if (sr % i == 0) :

c += 1

if c == 2 :

print("Square root is prime")

else :

print("Square root is not prime")

Output

Enter a number: 49

Square root is prime

Question 9

Write a short program to print first n odd numbers in descending order.

Solution

n = int(input("Enter n: "))

x=n*2-1

for i in range(x, 0, -2) :

print(i)

Output

Enter n: 5
9

Question 10

Write a short program to print the following series :

(i) 1 4 7 10 .......... 40.

(ii) 1 -4 7 -10 .......... -40

Solution

print("First Series:")

for i in range(1, 41, 3) :

print(i, end = ' ')

print("\nSecond Series:")

x=1

for i in range(1, 41, 3) :

print(i * x, end = ' ')

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")

print("(Enter 'q' to see the average)")

while True :

n = input()

if n == 'q' or n == 'Q' :

break

else :

sum += int(n)

count += 1

avg = sum / count

print("Average = ", avg)

Output

Enter numbers

(Enter 'q' to see the average)

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

a = int(input("Enter first side : "))

b = int(input("Enter second side : "))

c = int(input("Enter third side : "))

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

Enter first side : 10

Enter second side : 5

Enter third side : 10

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

a = int(input("Enter an integer: "))

if a % 10 == 4 :

print("ends with 4")

elif a % 10 == 8 :

print("ends with 8")

else :

print("ends with neither")

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

n = int(input("Enter a number greater than 20: "))

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

Enter a number greater than 20: 25

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:")

print("(Enter 'q' to see the result)")

l = input()

if l != 'q' and l != 'Q' :

l = int(l)

while True:

n = input()

if n == 'q' or n == 'Q' :

break

n = int(n)

if n > l :

l=n

print("Largest Number =", l)

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

n = int(input("How many numbers you want to enter? "))

if n > 1 :

l = int(input()) # Assume first input is largest

sl = int(input()) # Assume second input is second largest

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

print("Second Largest Number =", sl)

else :

print("Please enter more than 1 number")

Output

How many numbers you want to enter? 5

55

25

36

12

18

Second Largest Number = 36

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:")

print("(Enter 'q' to stop)")

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) :

print(n, "is a Palindrome Number")

else :

print(n, "is not a Palindrome Number")

Output

Enter numbers:

(Enter 'q' to stop)

67826

67826 is not a Palindrome Number

4321234

4321234 is a Palindrome Number

256894

256894 is not a Palindrome Number

122221

122221 is a Palindrome Number

Question 18

Write a complete Python program to do the following :


(i) read an integer X.

(ii) determine the number of digits n in X.

(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

x = int(input("Enter an integer: "))

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

Enter an integer: 2134

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 :

print(i, "is divisible by", m)

if i % 2 == 0 :

print(i, "is even")

else :

print(i, "is odd")

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

Write Python programs to sum the given sequences:

2/9 - 5/13 + 8/17 ...... (print 7 terms)

Solution

n = 2 #numerator initial value

d = 9 #denominator initial value

m = 1 #to add/subtract alternate terms

sum = 0

for i in range(7) :

t=n/d

sum += t * m

n += 3

d += 4

m *= -1

print("Sum =", sum)


Output

Sum = 0.3642392586003134

Question 20b

Write Python programs to sum the given sequences:

12 + 32 + 52 + ..... + n2 (Input n)

Solution

n = int(input("Enter the value of n: "))

i=1

sum = 0

while i <= n :

sum += i ** 2

i += 2

print("Sum =", sum)

Output

Enter the value of n: 9

Sum = 165

Question 21

Write a Python program to sum the sequence:

1 + 1/1! + 1/2! + 1/3! + ..... + 1/n! (Input n)


Solution

n = int(input("Enter the value of n: "))

sum = 0

for i in range(n + 1) :

fact = 1

for j in range(1, i) :

fact *= j

term = 1 / fact

sum += term

print("Sum =", sum)

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

n = int(input("Enter the value of n: "))

g1 = g2 = g3 = 0
for i in range(1, n + 1) :

age = int(input("Enter employee age: "))

#We have used chained comparison operators

if 26 <= age <= 35 :

g1 += 1

elif 36 <= age <= 45 :

g2 += 1

elif 46 <= age <= 55 :

g3 += 1

print("Employees in age group 26 - 35: ", g1)

print("Employees in age group 36 - 45: ", g2)

print("Employees in age group 46 - 55: ", g3)

Output

Enter the value of n: 10

Enter employee age: 45

Enter employee age: 53

Enter employee age: 28

Enter employee age: 32

Enter employee age: 34

Enter employee age: 49

Enter employee age: 30

Enter employee age: 38

Enter employee age: 33


Enter employee age: 53

Employees in age group 26 - 35: 5

Employees in age group 36 - 45: 2

Employees in age group 46 - 55: 3

Question 23a

Write programs to find the sum of the following series:

x - x2/2! + x3/3! - x4/4! + x5/5! - x6/6! (Input x)

Solution

x = int(input("Enter the value of x: "))

sum = 0

m=1

for i in range(1, 7) :

fact = 1

for j in range(1, i+1) :

fact *= j

term = x ** i / fact

sum += term * m

m = m * -1

print("Sum =", sum)

Output
Enter the value of x: 2

Sum = 0.8444444444444444

Question 23b

Write programs to find the sum of the following series:

x + x2/2 + x3/3 + ...... + xn/n (Input x and n both)

Solution

x = int(input("Enter the value of x: "))

n = int(input("Enter the value of n: "))

sum = 0

for i in range(1, n + 1) :

term = x ** i / i

sum += term

print("Sum =", sum)

Output

Enter the value of x: 2

Enter the value of n: 5

Sum = 17.066666666666666

Question 24a

Write programs to print the following shapes:


*

**

***

**

Solution

n = 3 # number of rows

# upper half

for i in range(n) :

for j in range(n, i+1, -1) :

print(' ', end = '')

for k in range(i+1) :

print('*', end = ' ')

print()

# lower half

for i in range(n-1) :

for j in range(i + 1) :

print(' ', end = '')

for k in range(n-1, i, -1) :

print('*', end = ' ')

print()

Output
*

**

***

**

Question 24b

Write programs to print the following shapes:

**

***

**

Solution

n = 3 # number of rows

# upper half

for i in range(n) :

for k in range(i+1) :

print('*', end = ' ')

print()

# lower half

for i in range(n-1) :
for k in range(n-1, i, -1) :

print('*', end = ' ')

print()

Output

**

***

**

Question 24c

Write programs to print the following shapes:

* *

* *

* *

Solution

n = 3 # number of rows

# upper half

for i in range(1, n+1) :

# for loop for initial spaces

for j in range(n, i, -1) :


print(' ', end = '')

#while loop for * and spaces

x=1

while x < 2 * i :

if x == 1 or x == 2 * i - 1 :

print('*', end = '')

else :

print(' ', end = '')

x += 1

print()

# lower half

for i in range(n-1, 0, -1) :

# for loop for initial spaces

for j in range(n, i, -1) :

print(' ', end = '')

#while loop for * and spaces

x=1

while x < 2 * i :

if x == 1 or x == 2 * i - 1 :

print('*', end = '')

else :

print(' ', end = '')


x += 1

print()

Output

**

* *

**

Question 24d

Write programs to print the following shapes:

**

* *

* *

* *

**

Solution

n = 4 # number of row

#upper half

for i in range(1, n+1) :

#while loop for * and spaces


x=1

while x < 2 * i :

if x == 1 or x == 2 * i - 1 :

print('*', end = '')

else :

print(' ', end = '')

x += 1

print()

#lower half

for i in range(n-1, 0, -1) :

#while loop for * and spaces

x=1

while x < 2 * i :

if x == 1 or x == 2 * i - 1 :

print('*', end = '')

else :

print(' ', end = '')

x += 1

print()

Output

**

* *

* *
* *

**

Question 25a

Write programs using nested loops to produce the following patterns:

AB

ABC

ABCD

ABCDE

ABCDEF

Solution

n=6

for i in range(n) :

t = 65

for j in range(i + 1) :

print(chr(t), end = ' ')

t += 1

print()

Output

AB
ABC

ABCD

ABCDE

ABCDEF

Question 25b

Write programs using nested loops to produce the following patterns:

BB

CCC

DDDD

EEEEE

Solution

n=5

t = 65

for i in range(n) :

for j in range(i + 1) :

print(chr(t), end = ' ')

t += 1

print()

Output

BB
CCC

DDDD

EEEEE

Question 25c

Write programs using nested loops to produce the following patterns:

22

444

6666

88888

Solution

for i in range(0, 10, 2):

for j in range(0, i + 1, 2) :

print(i, end = ' ')

print()

Output

22

444

6666

88888

Question 25d

Write programs using nested loops to produce the following patterns:


2

44

666

8888

Solution

for i in range(2, 10, 2) :

for j in range(2, i + 1, 2) :

print(i, end = ' ')

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('*', end = '')

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

Next higher number = 12

Highest number = 15

Solution

a = int(input("Enter first number: "))

b = int(input("Enter second number: "))

c = int(input("Enter third number: "))

if a < b and a < c :

small = a

if b < c :

middle = b

large = c

else :

middle = c
large = b

elif b < a and b < c :

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

print("Smallest number =", small)

print("Next higher number =", middle)

print("Highest number =", large)

Output

Enter first number: 10

Enter second number: 5


Enter third number: 15

Smallest number = 5

Next higher number = 10

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:

F = 9/5C + 32 and C = 5/9 (F 32).

Solution

temp = float(input("Enter Temperature: "))

unit = input("Enter unit('C' for Celsius or 'F' for Fahrenheit): ")

if unit == 'C' or unit == 'c' :

newTemp = 9 / 5 * temp + 32

print("Temperature in Fahrenheit =", newTemp)

elif unit == 'F' or unit == 'f' :

newTemp = 5 / 9 * (temp - 32)

print("Temperature in Celsius =", newTemp)

else :

print("Unknown unit", unit)

Output

Enter Temperature: 38

Enter unit('C' for Celsius or 'F' for Fahrenheit): C

Temperature in Fahrenheit = 100.4


Question 29

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 it is exactly -273.15, print that the temperature is absolute 0.

If the temperature is between -273.15 and 0, print that the temperature is below freezing.

If it is 0, print that the temperature is at the freezing point.

If it is between 0 and 100, print that the temperature is in the normal range.

If it is 100, print that the temperature is at the boiling point.

If it is above 100, print that the temperature is above the boiling point.

Solution

temp = float(input("Enter Temperature in Celsius: "))

if temp < -273.15 :

print("Temperature is invalid as it is below absolute zero")

elif temp == -273.15 :

print("Temperature is absolute zero")

elif -273.15 <= temp < 0:

print("Temperature is below freezing")

elif temp == 0 :

print("Temperature is at the freezing point")

elif 0 < temp < 100:

print("Temperature is in the normal range")

elif temp == 100 :


print("Temperature is at the boiling point")

else :

print("Temperature is above the boiling point")

Output

Enter Temperature in Celsius: -273.15

Temperature is absolute zero

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:

Enter the maximum value to display: 10

1=1

2 = 2 (prime)

3= 3 (prime)

4 = 2x2x

5 = 5 (prime)

6 = 2x3x

7 = 7 (prime)

8 = 2x2x2x

9 = 3x3x

10 = 2x5x

You might also like