Project Format
Project Format
SCHOOL
MORADABAD
COMPUTER SCIENCE
PROGRAMMING QUESTIONS
SUBJECT TEACHER:
Mr. Mudit
ACKNOWLEDGEMENT
I would like to express my heartfelt gratitude
to all those who contributed to the
completion of this project. This endeavour
wouldn’t have seen possible without the
support , guidance and encouragement I
received from various individuals and
organisations.
First and foremost , I extend my sincere
thanks to Mr. Mudit for their invaluable
guidance , patience and expertise
throughout this project. Their mentorship
and constant support in shaping our ideas
and steering us in the right direction.
Thank you everyone who directly or
indirectly supported me during this project.
Your contributions are truly invaluable and
greatly appreciated.
Saksham Narang
Class: 12th
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num = 7
if num < 0:
elif num == 0:
else:
Output:
Code:
# Program to check if a string is palindrome or not
my_str = 'aIbohPhoBiA'
my_str = my_str.casefold()
rev_str = reversed(my_str)
if list(my_str) == list(rev_str):
else:
Output:
Code:
# Python program to find H.C.F of two numbers
# define a function
if x > y:
smaller = y
else:
smaller = x
hcf = i
return hcf
num1 = 54
num2 = 24
Output:
Q4. Implement a program to count the number of vowels in a
given string
Code:
string = "counting vowels!"
vowels = "aeiouAEIOU"
Output:
5
Q5. Write a Python program to generate the Fibonacci series
up to n terms.
CODE:
# Program to display the Fibonacci sequence up to n-th term
n1, n2 = 0, 1
count = 0
if nterms <= 0:
elif nterms == 1:
print(n1)
else:
print("Fibonacci sequence:")
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
Output:
for n in range(num):
lst.append(numbers)
print("Maximum element in the list is :", max(lst), "\nMinimum element in the list is :", min(lst))
OUTPUT:
Q7. Create a program to count the frequency of each
element in a list
CODE:
# importing the module
import collections
OUTPUT:
{'A': 3, 'B': 3, 'C': 1, 'D': 2}
OUTPUT:
{1: 'a', 2: 'c', 4: 'd'}
Q9. Implement a Python program to sort a list of tuples
based on the second element.
CODE:
a = [(1, 3), (4, 1), (2, 2)]
OUTPUT:
Sorted List: [(4, 1), (2, 2), (1, 3)]
print(my_string.count(my_char))
OUTPUT:
2
Q11. Create a Python program to find all the substrings of a
given string.
CODE:
def get_all_substrings(input_string):
# base case 1: if the input string is empty, return an empty list
if len(input_string) == 0:
return []
# base case 2: if the input string contains only one character, return a list
with that character
elif len(input_string) == 1:
return [input_string]
# recursively call the function with the input string sliced from the
second character to the end
# and concatenate the resulting list of substrings with the list
generated from the current call
return output + get_all_substrings(input_string[1:])
OUTPUT:
['h', 'he', 'hel', 'hell', 'hello', 'e', 'el', 'ell', 'ello', 'l', 'll', 'llo', 'l', 'lo', 'o', 'e', 'el', 'ell',
'ello', 'l', 'll', 'llo', 'l', 'lo', 'o', 'l', 'll', 'llo', 'l', 'lo', 'o', 'l', 'lo', 'o', 'o']
CODE:
# Function to count number of characters, words, spaces and lines in a file
def counter(fname):
OUTPUT:
Number of words in text file: 25
Number of lines in text file: 4
Number of characters in text file: 91
Number of spaces in text file: 21
# use copyfile()
shutil.copyfile('first.txt','second.txt')
OUTPUT:
I Am The Contents Of First File
Geeks For Geeks!
Code:
class Student:
marks = []
def getData(self, rn, name, m1, m2, m3):
Student.rn = rn
Student.name = name
Student.marks.append(m1)
Student.marks.append(m2)
Student.marks.append(m3)
def displayData(self):
print ("Roll Number is: ", Student.rn)
print ("Name is: ", Student.name)
#print ("Marks in subject 1: ", Student.marks[0])
#print ("Marks in subject 2: ", Student.marks[1])
#print ("Marks in subject 3: ", Student.marks[2])
print ("Marks are: ", Student.marks)
print ("Total Marks are: ", self.total())
print ("Average Marks are: ", self.average())
def total(self):
return (Student.marks[0] + Student.marks[1] +Student.marks[2])
def average(self):
return ((Student.marks[0] + Student.marks[1] +Student.marks[2])/3)
s1 = Student()
s1.getData(r, name, m1, m2, m3)
s1.displayData()
OUTPUT:
Enter the roll number: 10
Enter the name: Mahesh Huddar
Enter the marks in the first subject: 20
Enter the marks in the second subject: 30
Enter the marks in the third subject: 25
Roll Number is: 10
Name is: Mahesh Huddar
Marks are: [20, 30, 25]
Total Marks are: 75
Average Marks are: 25.0
CODE:
# Created a Empty List
stack = []
def pop(stack):
if len(stack) == 0:
return None
else:
return stack.pop()
print("Initial Stack")
print(stack)
OUTPUT:
The Stack items are :
1
2
3
Initial Stack
[1, 2, 3]
Popped Element: 3
Q17. Write a Python program to create a database named
LibraryDB and a table named Books with columns: BookID
(INT, Primary Key), Title (VARCHAR), Author (VARCHAR), and
Price (FLOAT).
CODE:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword"
)
mycursor = mydb.cursor()
OUTPUT:
#If this page is executed with no error, you have successfully created a
database.
CODE:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE Books (BookID(INT,PRIMARY
KEY),Title(varchar),Aurthor(varchar),Price(FLOAT))")
OUTPUT:
#If this page is executed with no error, you have successfully created a table
named "customers".
conn = mysql.connector.connect(
host='localhost',
user='username',
password='password'
database='gfg'
)
# row created
cursor.execute(insert_st)
# save changes
conn.commit()
OUTPUT:
output:
ncert cs
ncert math
ncert eng
ANSWERS
i. SELECT(ISSUEDATE)[(ISSUEDATE)]
FROM MEMBER
WHERE aggregate> 2016-01-01
ORDER BY issuedate DESC;
ii. SELECT(BNAME)
FROM BOOK
WHERE book type= Fiction,
iii. SELECT(TYPE,NO.OF BOOKS)
FROM BOOK
WHERE book type=comic,literature,fiction;