Computer Science Project
Computer Science Project
c) Flow of Control :-
Q1) Write a program to print absolute value of a number(using Loops):
Ans) num = int(input("Enter the Number : "))
if num>=0:
print("Absolute value : ",num)
else:
num*=-1
print("Absolute value :
",num)
Q2) Program to calculate the factorial of a number
Ans) n = int(input("Enter the number : ")) fact = 1 a
= 1 while a<= n: fact*=a a+=1 print("The
factorial of",n,'is',fact)
Q3) Write a table of any number given by the user.(till 10)
Ans) n = int(input("Enter the number : ")) for i in
range(11):
print(n,"X",i,'=',n*i)
Q3) Write a program that takes the name and age of the user as input
and displays a message whether the user is eligible to apply for a
driving liceanse or not.(eligible age is 18 years)
ANS) name =input('Enter your name : ')
age = int(input("Enter your age if
age >=18:
print("You are eligible for driving liceanse.")
else:
print("You are not eligible for driving liceanse.")
Q4) Write a program that prints minimum and maximum of five
numbers entered by the user.
Ans) n = int(input("Enter the number : "))
min,max=n,n
for i in range(4):
n = int(input("Enter the number : "))
if n>max: max = n if n<min:
min=n
print("Maximum number :",max) print("Minimum
number :",min)
d) String Manipulation :-
Q1) Write a program which reverses a string and stores the reversed
string in a new string.
Ans) str = input("Enter the string: ")
newStr = ""
for ch in str :
newStr = ch + newStr
print(newStr)
Q2) Program to read a string and display it in reverse order one
character per line.
Ans) String1 = input(“Enter a String: ”)
print(“The”,string1,”in reverse order is:”) length
= len(string1)
for a in range(-1, (-length-1), -1):
print(string[a] )
Q3) Write a program that inputs a string that contains a decimal number
and prints out the decimal part of the number. For instance, if 515.8059
is given, the program should print out 8059. Ans) s = input(‘Enter a
String(a decimal number): ’)
t = s.partition(‘.’) print(“Given
string:”,s) print(“part after
decimal”, t[2] )
Q4) Write a program to input two strings. If string1 is contained in
string2, then create a third string with first four characters of string2'
added with word 'Restore'.
Ans) s1 = input(“Enter string 1 :”)
s2 = input(“Enter string 2 :”)
print(“Original strings:”,s1,s3)
if s1 in s2:
s3 = s2[0:4]+ “Restore”
print(“Final string:” ,s1,s3)
Q5) Write a program to input a string and check if it’s a palindrome
string using a string slice.
Ans) s = input(“Enter a string:”)
If (s==s[::-1]): print(s,”is
a Palindrome.”) else:
print(s,”isNOT a Palindrome.”)
Q6) Write a program to input an integer and check if it contains any 0 in
it.
Ans) n = int(input(“Enter a number:”))
s = str(n)
if ‘0’ is s:
print(“There’s a 0 in”,n)
else:
print(“No 0 in”,n)
e) List Manipulation :-
Q1) Program to print elements of a list [ 'q', 'w', 'e', 'r', 't', 'y' ] in separate
lines along with element's both indexes (positive and negative).
Ans) L = [‘q’, ‘w’, ‘e’, ‘r’, ‘t’, ‘y’]
length = len(L) for a
in range(length):
print(“At indexes”,a, “and”, (a – length), “element:”, L[a])
Q2) Write a program display the lists. to create a copy of a list. In the
list's copy, add 10 to its first and last elements. Then display the lists.
Ans) L1 = [17, 24 ,15, 30, 34, 27]
L2 = L1.copy() print(“Original
list:”,L1) print(“Created copy of
the list:”,L2)
L2[0] += 10 L2[-1] += 10 print(“Copy of
the list after changes :”,L2)
print(“Original list :”, L1)
Q3) Create a list named friends and include elements like name, date of
birth, lucky number, favorite colour, weight, and height. Ans) name =
input('Enter Name:') lucky_no = int(input('Enter Lucky Number:'))
fav_col = input('Enter favorite color:') w = int(input('Enter weight:')) h
=int(input('Enter height:')) friends=[]
friends.append([name,lucky_no,fav_col,w,h])
print(friends)
Q4) Create a list of the following numbers and print them with their
index. [22,45,67,89,6,1,34,56,89,10] Ans) l =
[22,45,67,89,6,1,34,56,89,10]
print('Index – [0]: ',l[0])
print('Index – [1]: ',l[1])
print('Index – [2]: ',l[2])
print('Index – [3]: ',l[3])
print('Index – [4]: ',l[4])
print('Index – [5]: ',l[5])
print('Index – [6]: ',l[6])
print('Index – [7]: ',l[7])
print('Index – [8]: ',l[8])
print('Index – [9]: ',l[9])
Q5) Create a list of your friends’ names and print them.
Ans) list_frnds=
['Anurag','Bhargav','Chandrkant','Harshad','Jayesh','Mithun']
for i in range(0,len(list_frnds)):
print('Freind[',i,']:',list_frnds[i])
Q6) Write some lines about “Bharat is best” and make a list of words
used in the input.
Ans) w = 'Bharat is Best among all the countries'
l= w.split()
print(l)
f) Tuples:-
Q1) Write a program to create a tuple with a single input value.
Ans) t1 = eval(input(“enter input for tuple: ”)) print(“Created
tuple is:”, t1)
Q2) Write a program to print a tuple's first three and last three
elements in the following manner: 1st element, last element
2nd element, 2nd last element
3rd element, 3rd last element
Ans) t1 = eval(input("Enter input for tuple:
")) print(t1[0], t1[-1]) print(t1[1], t1[-2])
print(t1[2], t1[-3])
Q3) Program to print elements of a tuple ('Hello', "Isn't", "Python', 'fun',
'?') in separate lines along with element's both indexes (positive and
negative).
Ans) T = ('Hello', "Isn't", "Python', 'fun', '?')
length = len(T)
for a in range(length):
print(‘At indexes’,a, ‘and’, (a-lenth), ‘element:’,t[a])
Q4) Write a program to check if all the elements of a tuple are in
descending order or not.
Ans) tup=eval(input('Enter a tuple:')) if
sorted(tup, reverse=True)==list(tup):
print('Tuple is sorted in descending order') else:
print('Tuple is not sorted in descending order:')
Q5) Write a program to create a nested tuple to store roll number,
name and marks of students. Ans) tup= () while True :
roll = int(input("Enter a roll number : ")) name
= input("Enter name :") mark = input("Enter
marks :") tup += ( (roll,name,mark ),) user =
input("Do you want to quit enter yes/no =")
if user == "yes":
print(tup) break