ASSIGNMENT
OF
COMPUTER
SCIENCE
NAME: SAIYED AREESH AQUEDUS
CLASS: XI (A)
TEACHER’S SIGN:
1. Program to obtain length and breadth of a rectangle and
calculate its area.
length=float(input("Enter Length of the rectangle: "))
breadth=float(input("Enter Breadth of the rectangle: "))
area= length*breadth
print("The Area of the rectangle is",area)
Output :-
Enter Length of the rectangle: 12
Enter Breadth of the rectangle: 15
The Area of the rectangle is 180.0
2. Write a program to input two numbers and swap them.
a=int(input("Enter first number: "))
b=int(input("Enter second number: "))
print("Entered numbers are num1 =",a,"and num2 =",b)
a,b=b,a
print("Numbers after swapping are num1 =",a,"and num2 =",b)
Output:-
Enter first number: 65
Enter second number: 89
Entered numbers are num1 = 65 and num2 = 89
Numbers after swaping are num1 = 89 and num2 = 65
3. Program that takes a number and checks whether the given
number is odd or even.
num=int(input("Enter a number:
")) if num%2==0:
print("The number is even")
else:
print("The number is odd")
Output :-
Enter a number: 76
The number is even
Enter a number: 43
The number is odd
4. Program to accept three integers and print the largest
of three. Make use of only if statement.
x=y=z=0
x=float(input("Enter first number: "))
y=float(input("Enter second no.: "))
z=float(input("Enter third no. "))
max=x
if y>max:
max=y
if z>max:
max=z
print("Largest no. is", max)
Output :-
Enter first number: 45
Enter second no.: 37
Enter third no. 90
Largest no. is 90.0
Enter first number: 87
Enter second no.: 65
Enter third no. 23
Largest no. is 87.0
5. Program to test divisibility of a number with another number
(i.e if a number is divisible by another number.)
number1=int(input("Enter first number :"))
number2=int(input("Enter second number :"))
remainder=number1%number2
if remainder==0:
print(number1,"is divisible by",number2)
else:
print(number1,"is not divisible by",number2)
Output:-
Enter first number : 123
Enter second
number :3 123 is
divisible by 3
Enter first number : 243
Enter second
number :7 243 is not
divisible by 7
6. Program to display a menu for calculating area of a circle or
circumference of a circle.
radius=float(input("Enter radius of the circle :"))
print("1.Calculate Area")
print("2.Calculate Perimeter")
choice=int(input("Enter your choice(1or2):"))
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)
Output :-
Enter radius of the circle :
7 1.Calculate Area
2.Calculate Perimeter
Enter your choice(1or2):2
Perimeter of circle with radius 7.0 is 43.98226
Enter radius of the circle :
7 1.Calculate Area
2.Calculate Perimeter
Enter your choice(1or2):1
Area of circle with radius 7.0 is 153.93791
7. Program to print whether a given character is an uppercase
or lowercase character or a digit or any other character.
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.")
Output :-
Enter a character : 7
You entered a digit.
Enter a character: v
You entered a lower case character
Enter a character : @
You entered a special character.
Enter a character: D
You entered an upper case character.
8. Program to calculate and print roots of a quadratic equation
Ax²+ Bx+C=0.
import math
print("For quadratic equation, ax**2+bx+c, enter coefficients
below")
a=int(input("Enter a:"))
b=int(input("Enter b:"))
c=int(input("Enter c:"))
if a==0:
print("value of a cannot be zero, 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("Root1=",root1,", Root=",root2)
elif delta==0:
root1= -b/(2*a)
print("roots are REAL and EQUAL")
print("root1=",root1,", root2=",root1)
else:
print("roots are COMPLEX and IMAGINARY")
Output :-
1. For quadratic equation, ax**2+bx+c, enter coefficients below
Enter a: 1
Enter b:7
Enter c:10
roots are REAL and UNEQUAL
Root1= -2.0 , Root= -5.0
2. For quadratic equation, ax**2+bx+c, enter coefficients below
Enter a: 2
Enter b:4
Enter c:2
roots are REAL and EQUAL
root1= -1.0 , root2= -1.0
3. For quadratic equation, ax**2+bx+c, enter coefficients below
Enter a: 7
Enter b:6
Enter c:9
roots are COMPLEX and IMAGINARY
9. Program to print sum of natural numbers between 1 to
7. Print the sum progressively, i.e after adding each natural
number, print sum so far.
sum=0
for n in range(1,8):
sum+=n
print("Sum of natural numbers <=",n,"is",sum)
Output :-
Sum of natural numbers <= 1 is 1
Sum of natural numbers <= 2 is 3
Sum of natural numbers <= 3 is 6
Sum of natural numbers <= 4 is 10
Sum of natural numbers <= 5 is 15
Sum of natural numbers <= 6 is 21
Sum of natural numbers <= 7 is 28
10. Program to calulate factorial of a number.
num=int(input("Enter a number:"))
fact=1
a=1
while a<=num:
fact*=a
a+=1
print("The factorial of",num,"is",fact)
Output :-
Enter a number: 11
The factorial of 11 is 39916800
Enter a number: 7
The factorial of 7 is 5040
11. Program to illustrate the difference between BREAK &
CONTINUE statements.
print("The loop with 'break' produces output as :")
for i in range(1,11):
if i%3==0:
break
else:
print(i)
print("The loop with 'continue' produces output as :")
for i in range(1,11):
if 1%3==0:
continue
else:
print (i)
Output :-
The loop with 'break' produces output as :
1
2
The loop with 'continue' produces output as :
1
2
3
4
5
6
7
8
9
10
12. Program that searches for prime numbers from 15 to 25.
for num in range(15,25):
for i in range(2,num):
if num%i==0:
j=num/i
print("Found a factor(",i,")for",num)
break
else:
print(num,"is a prime number")
Output :-
Found a factor( 3 )for 15
Found a factor( 2 )for 16
17 is a prime number
Found a factor( 2 )for 18
19 is a prime number
Found a factor( 2 )for 20
Found a factor( 3 )for 21
Found a factor( 2 )for 22
23 is a prime number
Found a factor( 2 )for 24
13. Write a program that displays options for inserting or
deleting elements in a list. If the user chooses a deletion option,
display a submenu and ask if element is to be deleted with
value or by using its position or a list slice is to deleted.
val=[17,23,18,19]
print("The list is:",val)
while True:
print("Main Menu")
print("1.Insert")
print("2.Delete")
print("3.Exit")
ch=int(input("Enter your choice 1/2/3:"))
if ch==1:
item=int(input("Enter item :"))
pos=int(input("Insert at which position?"))
index=pos-1
val.insert(index,item)
print("Success! List now is :",val)
elif ch==2:
print("Deletion Menu")
print("1.Delete using Value")
print("2.Delete using index")
print("3.Delete a sublist")
dch=int(input("Enter choice (1 or 2 or 3):"))
if dch==1:
item=int(input("Enter item to be deleted:"))
val.remove(item)
print("List now is : ",val)
elif dch==2:
index=int(input("Enter index of item to be deleted:"))
val.pop(index)
print("List now is :",val)
elif dch==3:
l=int(input("Enter lower limit of list slice to be deleted:"))
h=int(input("Enter upper limit of list slice to be
deleted:"))
del val[l:h]
print("List now is :",val)
else:
print("valid choices are 1/2/3 only.")
elif ch==3:
break;
else:
print("valid choices are 1/2/3 only.")
Output :-
The list is: [17, 23, 18, 19]
Main Menu
1.Insert
2.Delete
3.Exit
Enter your choice 1/2/3: 1
Enter item : 75
Insert at which position? 3
Success! List now is : [17, 23, 75, 18, 19]
Main Menu
1.Insert
2.Delete
3.Exit
Enter your choice 1/2/3: 2
Deletion Menu
1.Delete using Value
2.Delete using index
3.Delete a sublist
Enter choice (1 or 2 or 3):1
Enter item to be deleted: 23
List now is : [17, 75, 18, 19]
Main Menu
1. Insert
2. Delete
3.Exit
Enter your choice 1/2/3:2
Deletion Menu
1.Delete using Value
2.Delete using index
3.Delete a sublist
Enter choice (1 or 2 or 3): 2
Enter index of item to be deleted: 2
List now is : [17, 75, 19]
Main Menu
1.Insert
2.Delete
3.Exit
Enter your choice 1/2/3: 2
Deletion Menu
1.Delete using Value
2.Delete using index
3.Delete a sublist
Enter choice (1 or 2 or 3):3
Enter lower limit of list slice to be deleted:1
Enter upper limit of list slice to be deleted:3
List now is : [17]
Main Menu
1.Insert
2.Delete
3.Exit
Enter your choice 1/2/3: 3
14. Program to search for an element in a given list of
numbers. lst=eval(input("Enter list:"))
length=len(lst)
element=int(input("Enter element to be searched for:"))
for i in range(0,length):
if element==lst[i]:
print(element,"found at index",i)
break
else:
print (element, "not found in given list")
Output :-
Enter list:[32,4,56,79,45,12,65,90,7,57,49,34,27,88]
Enter element to be searched for: 49
49 found at index 10
Enter list:[32,4,56,79,45,12,65,90,7,57,49,34,27,88]
Enter element to be searched for: 27
27 found at index 12
15. Write a program to input two lists and display the
maximum element from the elements of both the lists
combined along with its index in its list.
lst1=eval(input("Enter list1:"))
lst2=eval(input("Enter list2:"))
mx1=max(lst1)
mx2=max(lst2)
if mx1>=mx2:
print(mx1,"the maximum value is in list1 at
index",lst1.index(mx1))
else:
print(mx2,"the maximum value is in list2 at
index",lst2.index(mx2))
Output :-
Enter list1: [12,32,76,91,45,67,84]
Enter list2: [54,32,27,93,47,90,66]
93 the maximum value is in list2 at index 3
Enter list1: [9,66,57,41,90,79,55,12]
Enter list2: [18,78,34,51,95,69,78,8]
95 the maximum value is in list2 at index 4
16. What will be the types of t1, t2, 13 created as per the code
given below considering the three inputs of eg. 1
t1=eval(input("Enter input for tuple1:"))
t2=eval(input("Enter input for tuple2:"))
t3=eval(input("Enter input for tuple3:"))
print("Type of t1:", type (t1))
print("Type of t2:", type (t2))
print("Type of t3:", type (t3))
Output :-
Enter input for tuple1:"abcdef"
Enter input for tuple2: 3,4,5,6
Enter input for tuple3: [11,12,13]
Type of t1: <class 'str'>
Type of t2: <class 'tuple'>
Type of t3: <class 'list'>
17.Program to print elements of a tuple ('Hello', 'isn't, 'Python'
, 'Fun', '?') in separte lines along with element's both indexes
(positive and negative).
T=('Hello','Isn't','Python','fun','?')
length=len(T)
for a in range(length):
print('At indexes',a,'and',(a-length),'element:',T[a])
Output :-
At indexes 0 and -5 element: Hello
At indexes 1 and -4 element: Isn't
At indexes 2 and -3 element: Python
At indexes 3 and -2 element: fun
At indexes 4 and -1 element: ?
18.A tuple t1 stores (11,21,31,42,51) where its second last
element is mistyped, write a program to correct its second last
element as 41.
t1=(11,21,31,42,51)
tl1=list(t1)
tl1[-2]=41
t1=tuple(tl1)
print("Modified tuple:",t1)
Output :-
Modified tuple: (11, 21, 31, 41, 51)
19. Write a progarm to read roll number and marks of four
students and create a dictionary from it having roll number as
keys.
rno=[]
mks=[]
for a in range(4):
r,m=eval(input("Enter Roll No.,Marks:"))
rno.append(r)
mks.append(m)
d={rno[0]:mks[0],rno[1]:mks[1],rno[2]:mks[2],rno[3]:mks[3]}
print("Created dictionary")
print(d)
Output :-
Enter Roll No.,Marks: 4,98
Enter Roll No.,Marks:17,87
Enter Roll No.,Marks:3,79
Enter Roll No.,Marks:8,90
Created dictionary
{4: 98, 17: 87, 3: 79, 8: 90}
20. Write a program to add new students' roll numbers and
marks in the dictionary M created with roll number as the keys
and marks as the values.
M={}
n=int(input("How many students?"))
for a in range(n):
r,m=eval(input("Enter Roll No.,Marks:"))
M[r]=m
print("Created dictionary")
print(M)
ans='y'
while ans=='y':
print("Enter details of new student")
r,m=eval(input("Roll No.,Marks:"))
M[r]=m
ans=input("More students?(y/n):")
print("Dictionary after adding new students")
print(M)
Output :-
How many students? 3
Enter Roll No.,Marks: 2, 78
Enter Roll No.,Marks: 12, 84
Enter Roll No.,Marks: 4, 90
Created dictionary
{2: 78, 12: 84, 4: 90}
Enter details of new student
Roll No.,Marks: 8, 67
More students? (y/n) :y
Enter details of new student
Roll No.,Marks: 7, 98
More students? (y/n) :y
Enter details of new student
Roll No.,Marks: 18, 45
More students? (y/n) :n
Dictionary after adding new students
{2: 78, 12: 84, 4: 90, 8: 67, 7: 98, 18: 45}