0% found this document useful (0 votes)
27 views

Exercise Conditional Operator-1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Exercise Conditional Operator-1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

CHAPTER- CONDITIONAL OPERATOR

CLASS-XI
EXERCISE- (TYPE C )

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

a = int(input("enter the length in centimeter = "))

if a < 0 :

print("invaild")

else :

print("length in inch = ",a/2.54)

Q 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 per user how many items they are buying and print the total cost .

item = int(input("enter the total number of item = "))

if item < 10 :

print("total cost = ",item * 120 , "rs")

elif item>10 and item < 99 :

print("total cost = ",item * 100, "rs")

else :

print("total cost = ",item * 70 , "rs")

Q 3 = write a program that the user for two number and prints close if the number are
within 0.001 of each other and not close otherwise .

Q 4 = a year is a leap year if it is divisible by 100 are not leap year unless they are
also divisible by 400 . write a program that asks the

user for a year and print out weather it is a leap year or not .
Q 5 = write a program to input length of three side of a triangle . then check if these
side will form a triangle or not .

a = int (input ("Enter the frist 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 ")

Q 6 = write a short program to input a digit(1 -10) and print it in words .

a = int (input ("enter the digit (1-10) ="))


if a ==1 :
print ("one ")
elif a ==2 :
print ("two")
elif a ==3 :
print ("three")
elif a == 4:
print ("four ")
elif a == 5:
print ("five")
elif a == 6:
print ("six ")
elif a == 7:
print ("seven")
elif a == 8:
print ("eight ")
elif a == 9:
print ("nine")
elif a == 10 :
print ("ten")
else:
print ("digit is out of range ")

Q 7 = write a short program to check weather square root of a number is prime or not .

num = int(input("enter a number = "))

import math

a = int(math.sqrt(num))
b = 0

for i in range(2,a):

if a % i == 0 :

b = b +1

if b == 0 :

print(a,"is a prime number ")

else :

print(a,"is not prime number ")

Q 8 = write a short program to print first n odd number in descending order .

n = int(input("enter n term of odd number = "))

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

print(i,end=" , ")

Q 9 = write a short program to print the following series :


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

(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=" , ")
Q 10 = write a short program to find average of list of number entered through
keyboard . ‘10’ yes
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

Q11 = write a program to input 3 sides of triangle and print whether it is an equilateral
, scalene or isosceles triangle .

side1 = int(input("enter 1st side of triangle = "))


side2 = int(input("enter 2nd side of triangle = "))
side3 = int(input("enter 3rd side of triangle = "))
if side1 == side2 == side3 :
print("it is a equilateral triangle ")
elif side1== side2!=side3 or side1!=side2==side3 or side1==side3!=side2 :
print("it is a isosceles triangle ")
elif side1 != side2 != side3 :
print("it is a scalene triangle ")

Q 12 = write a program to take an integer “a” as an input and check whether it ends
with 4 or 8 .if its ends with 4 , print “ends with 4 “, if it end with 8 ,print “ends with 8”,
otherwise print “ ends with neither ” .

a = int(input("enter a number = "))


if a % 10 == 4 :
print(a,"end with 4")
elif a% 10 == 8 :
print(a,"end with 8 ")
else :
print(a,"end with neither ")

Q 13 =write a program to take N(N>20) as an input from the user . print number from
11 to N .when the number is multiple of 3 ,print , “tipsy ” . when it is multiple of 7 print
“topsy ”.when it is a multiple of both print “tipsyTopsy”.
n = int(input("enter a number greater than 20 = "))
for i in range(11,n+1):
if i % 3 == 0 and i % 7==0 :
print(i,"tipsytopsy")
elif i % 3==0 :
print(i,"tipsy")
elif i % 7==0 :
print(i,"topsy")

Q 14 = write a short program to find largest number of the list of number entered
through keyboard .

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

Q 15 = write a program to input N number and then print the second largest number .

largest = 0
second = 0
while True:
a=int(input("enter a number (for quit enter 0 (zero) )="))
if a == 0 or A==0:
break
else:
if a >largest:
largest = a
elif a >second:
second = a
print(second,"is second biggest number ")

Q 16 = Given list of integer, write a program to find those which are palindromes.

while True :
a = input("enter a number (for quit enter q = ")
if a == "q" or a== "Q" :
break
else :
reverse = a[ -1 : - len(a) - 1 : - 1]

if a == reverse :
print(a,"is polindromes of ",reverse)
else :
print(a,"is not polindromes of ",reverse)
Q 17 = write a complete python program to do the following ;
(a) read an integer X .
(b) determine the number of digit n in X .
(c)from an integer Y that has the number of digit n at tens place and the most significant
Digit of X at ones place .
(d)output Y

x = input("enter a number = ")


n = len(x)
y = str(n) + x[0]
print("new number = ",y )

Q 18 =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 .
n = int(input("enter the n number = "))
m = int(input("enter m as divisor = "))
for i in range(1,n):
if i % m == 0 :
if i % 2 == 0 :
print(i,"is divisible m and this is even number ")
else :
print(i,"is divisible m and this is odd number ")

Q 19 = write python program to sum the given sequences :


(a): 2/9 – 5/13+18/17………(print 7 th term )
(b): 12 + 3 2 + 52+…….+n2 (input n)

a)=
a= 2
b = 9
sum = 2/9
for i in range(6):
a = a + 3
b = b + 4
if i % 2 == 0 :
sum = sum - a/b
else :
sum = sum + a/b
print("sum of sequensumes = ",sum)

(b)=
n = int(input("enter the n term = "))
sum = 0
for i in range(1,n+1):
sum = sum + (i ** 2)
print("sum of sequence = ",sum )
Q 20 = write a python program to sum the sequence:
1 + 1/1! + 1/2! + 1/3! + …….. + 1/n!
n = int(input("enter a nth term = "))
fact = 1
sum = 1
for i in range(1,n+1):
fact = fact * i
sum = sum + 1/fact
print("sum of sequence = ",sum)

Q 21 = write a program to accept the age of an employees and count the number of
person in the following age group .
(a) 26-35
(b) 36-45
(c) 46-55

a = 0
b = 0
c = 0
while True :
emp = int(input("enter the age of employees (enter 0 (zero) for quit )=
"))
if emp == 0 :
break
else :
if 25 < emp and emp < 36 :
a = a + 1
if 35 < emp and emp < 46 :
b = b + 1
if 45 < emp and emp < 56 :
c = c + 1
print("the age of employees 26 - 35 = ",a)
print("the age of employees 36 - 45 = ",b)
print("the age of employees 46 - 55 = ",c)

Q 22 = write a program to find the sum of the following series :


(a) x – x2 /2! + x3/3! …….. – x6/6!
(b) x + x2/2 ……… + xn/n (input x and n both)

(a)=
x = int(input("enter a term = "))
fact = 1
sum = 0
for i in range(1,7):
fact = fact * i
if i % 2 == 0 :
sum = sum - (x**i)/fact
else :
sum = sum + (x**i)/fact
print("sum of series = ",sum)

(b)=
x = int(input("enter a term = "))
sum = 0
for i in range(1,x+1):
sum = sum + x**i/i
print("sum of series = ",sum)

Q 23 = write program to print the following shapes :


a.
A =
for i in range(7):
for j in range(7):
if i < 4 :
if j >=3- i and j <= 3+i :
if i % 2 == j % 2 :
print(" ",end="")
else :
print("*",end="")
else :
print(" ",end="")
else :
if j <= 9-i and j >= i - 3 :
if i % 2 == j % 2 :
print(" ",end="")
else :
print("*",end="")
else :
print(" ",end="")
print()

B =

for i in range(1,6):
if i < 4 :
print("*" * i )
else :
print("*" * (6-i))
print()

C =

for i in range(5):
for j in range(5):
if i < 3 :
if j == 2 - i or j == 2 + i :
print("*" , end="")
else :
print(" " , end = "")
else :
if j == i-2 or j == 6 - i :
print("*" , end="")
else :
print(" " , end = "")
print()

D =

for i in range(7):
for j in range(4):
if i == j or j == 6-i :
print("*" , end="")
else :
if j == 0 :
print("*" , end = "")
else :
print(" ",end="")
print()
Q 24 = write programs using nested loops to produce the following pattern :
(i) A
AB
ABC
ABCD
ABCDE
ABCDEF

(ii) 0
22
444
8888

( I )

for i in range(6):
for j in range(6):
if i >= j :
print(chr(65+j)+" ",end="")
print()

(II)

for i in range(4):
if i == 0 :
print(0)
else:
print(str(2**i)*(i+1) )

Q 25 = write a program using nested loops to produce rectangle of *’s with 6 rows and
20 *’s per row .

for i in range(6):

for j in range(20):
if i == 0 or i == 5 :
print("*",end=" ")
elif j == 0 or j == 19 :
print("*",end=" ")
else :
print(" ",end=" ")
print()

Q 26 = given three number A ,B and C . write a program to write their values in an


ascending order . for example A= 12,B=10 and C = 15 ,

your program should print out :


Smallest number = 10
Next higher number = 12
Highest number = 15

a = int(input("enter 1st number = "))


b = int(input("enter 2nd number = "))
c = int(input("enter 3rd number = "))
if a > b and b > c :
max,mid,low = a,b,c

elif b > a and a > c :


max,mid,low = b,a,c

elif c > b and b > a :


max,mid,low=c,b,a

elif c > b and c > a and a > b :


max,mid,low=c,a,b

elif b > c and b > a and a<c :


max,mid,low=b,c,a

elif a> b and a > c and b<c :


x,mid,low= a,c,b

print("smallest number ",low)


print("next highest number ",mid)
print("hight number ",max)

Q 27 = write a python script to input temperature . then ask them what unit , Celsius or
Fahrenheit , the temperature is in . your program should convert the temperature to
other unit . the conversions are F = 9/5 C + 32 and C= 5/9(F – 32).

temp = float(input("enter temperature = "))

unit = input("enter unit of temperature ( c or f ) =")

if unit=="F" or unit == "f" :

c = (5/9)*(temp - 32)

print("temperatrue in celsuis = ",c,"C")

elif unit=="C" or unit == "c" :

f = (9/5)*temp + 32

print("temperatrue in fahrenheit = ",f,"F")

else :
print("invaild unit ")
Q 28 = ask the user to enter a temperature in Celsius . the program should print a
message based on the temperature :
(i)if the temperature is less than -273.15 print that the temperature is invalid because it
is below absolute zero :
(ii)if is exactly -273.15 ,print that the temperature is absolute 0 .
(iii)if the temperature is between -273.15 and 0 ,print that the temperature is below
freezing .
(iv)if it is 0 ,print that the temperature is at the freezing point .
(v)if it is between 0 and 100 , print that the temperature is in the normal range .
(vi)if it is 100 ,print that the temperature is at the boiling point .
(vii) if it above 100 , print that the temperature is above the boiling point .

temp = float(input("enter temperature in celsius= "))


if temp < -273.15 :
print ("the temperature is invalid ")
elif temp == -273.15 :
print ("the temperature is absolute 0 ")
elif temp > -273.15 and temp <0:
print ("the temperature is below freezing ")
elif temp == 0 :
print ("the temperature is at the freezing point")
elif 0< temp <100 :
print ("the temperature is in the normal range")
elif temp == 100:
print ("the temperature is at the boiling point")
elif temp > 100:
print (" the temperature is above the boiling point ")

You might also like