Lokesh

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Certificate

This is to certify that "Lokesh kumar" student of


class 11th 'A' has successfully completed their
computer science project under the guidance of
"Mr. Parveen kumar".
Acknowledgement

I am immensely grateful to my Computer Science


teacher Mr. Parveen Kumar for entrusting me with
this project. Their guidance and support have been
indispensable throughout every step. Their
expertise has not only shaped the direction of this
endeavor but has also deepened my understanding of
the subject. I am thankful for their encouragement,
patience, and belief in my potential. Their
mentorship has been a source of inspiration, driving
me to push my boundaries and strive for excellence.
Without their unwavering support and guidance, this
project would not have reached its fruition. Thank
you for this invaluable opportunity and guidance
Q1 . Input a welcome message and display it.

Solve. # Input name from the user


name = input("Enter your name: ")

# Display the welcome message


print("Welcome,", name, "!")
Q2. Input two numbers and display the larger/smaller
number.
Solve. # Input two numbers from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

# Compare the numbers to find the larger and smaller


ones
# Find the largest and smallest numbers among the
three
smallest = min(num1, num2, num3)

# Display the largest and smallest numbers


print("Largest number:", largest)
print("Smallest number:", smallest)
Q.3 input three number and display the largest
smaller number.

Solve. #Input three numbers from the user


num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number:
"))
num3 = float(input("Enter the third number: "))

# Find the largest number


largest = max(num1, num2, num3)

# Find the smallest number


smallest = min(num1, num2, num3)

# Display the largest and smallest numbers


print("The largest number is:", largest)
print("The smallest number is:", smallest)
Q 4.(a) Generate the following patterns using nested
loops:
*
**
***
****
*****

Solve 1 . #Pattern 1
num_rows = 5

# Outer loop to iterate over rows


for i in range(1, num_rows + 1):
# Inner loop to print stars in each row
for j in range(1, i + 1):
print("*", end="")
# Move to the next line after printing stars for
the current row
print()
Q 4.(b) Generate the following patterns using nested
loops:
12345
1234
123
12
1
Solve. for i in range(5, 0, -1): # Loop for the
rows in reverse order
for j in range(1, i + 1): # Loop for the
columns
print(j, end="")
print()
Q 4.(c) Generate the following patterns using nested
loops:
A
AB
ABC
ABCD
ABCDE

Solve. for i in range(65, 70):


for j in range(65, i + 1): # Loop for the
columns
print(chr(j), end="")
print()
Q.5(a) Write a program to input the value of x and n
and print the sum of the following series:
1 + x + x2 + x3 ….. + xn
Solve a. def compute_series_sum(x, n):
series_sum = 0
for i in range(n + 1):
series_sum += x ** i
return series_sum

# Input the value of x and n


x = float(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))
# Compute the sum of the series
result = compute_series_sum(x, n)
# Print the result
print("Sum of the series:", result)
(b).1 – x + x2 – x3 ……. xn
Solve (b). def series_sum(x, n):

sum = 0

sign = 1

for i in range(n + 1):

term = sign * (x ** i)

sum += term

sign *= -1 # Change the sign for the next term

return sum

# Input the value of x and n

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

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

result = series_sum(x, n)

print("Sum of the series:", result)


(c). x + x2/2 + x3/3 + …… + xn/n
Solve c. 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)


(d).x+ x2/2! + x3/3! ….. + xn/n!
Solve(d) def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)

def series_sum(x, n):


sum = 0
for i in range(1, n + 1):
term = (x ** i) / factorial(i)
sum += term
return sum

# Input the value of x and n


x = float(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))
result = series_sum(x, n)
print("Sum of the series:", result)
Q 6. Determine whether a number is a perfect
number, an Armstrong number or a palindrome.
Solve. # Function to check if a number is a perfect
square
def is_perfect_square(num):
square_root = int(num ** 0.5)
return square_root * square_root == num

# Function to check if a number is an Armstrong


number
def is_armstrong_number(num):
digit_sum = 0
temp = num
num_digits = len(str(num))

while temp > 0:


digit = temp % 10
digit_sum += digit ** num_digits
temp //= 10
return digit_sum == num

# Function to check if a number is a palindrome


def is_palindrome(num):
return str(num) == str(num)[::-1]

# Main function to test the above functions


def main():
num = int(input("Enter a number: "))

if is_perfect_square(num):
print(num, "is a perfect square.")
else:
print(num, "is not a perfect square.")

if is_armstrong_number(num):
print(num, "is an Armstrong number.")
else:
print(num, "is not an Armstrong number.")

if is_palindrome(num):
print(num, "is a palindrome.")
else:
print(num, "is not a palindrome.")

if __name__ == "__main__":
main()
Q7.Input a number and check if the number is a
prime or composite number
Solve.
def is_prime(num):
if num <= 1:
return False
elif num <= 3:
return True
elif num % 2 == 0 or num % 3 == 0:
return False
i=5
while i * i <= num:
if num % i == 0 or num % (i + 2) == 0:
return False
i += 6
return True

def main():
num = int(input("Enter a number: "))
if is_prime(num):
print(num, "is a prime number.")
else:
print(num, "is a composite number.")

if __name__ == "__main__":
main()
Q8.Display the terms of a Fibonacci series.
Solve.
def fibonacci_series(limit):
fib_series = []
a, b = 0, 1
while a <= limit:
fib_series.append(a)
a, b = b, a + b
return fib_series

def main():
limit = int(input("Enter the limit for the Fibonacci
series: "))
fib_series = fibonacci_series(limit)
print("Fibonacci series up to", limit, ":", fib_series)

if __name__ == "__main__":
main()
Q9. Compute the greatest common divisor and least
common multiple of two integers.
Solve.
def gcd(a, b):
while b:
a, b = b, a % b
return a

def lcm(a, b):


return (a * b) // gcd(a, b)

def main():
num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))

gcd_result = gcd(num1, num2)


lcm_result = lcm(num1, num2)
print("Greatest Common Divisor (GCD):",
gcd_result)
print("Least Common Multiple (LCM):",
lcm_result)

if __name__ == "__main__":
main()
Q10 . Count and display the number of vowels,
consonants, uppercase, lowercase characters in
string.
Solve.
def count_characters(string):
vowels = 0
consonants = 0
uppercase = 0
lowercase = 0

for char in string:


if char.isalpha():
if char.lower() in 'aeiou':
vowels += 1
else:
consonants += 1
if char.isupper():
uppercase += 1
elif char.islower():
lowercase += 1

return vowels, consonants, uppercase, lowercase

def main():
string = input("Enter a string: ")
vowels, consonants, uppercase, lowercase =
count_characters(string)

print("Number of vowels:", vowels)


print("Number of consonants:", consonants)
print("Number of uppercase characters:",
uppercase)
print("Number of lowercase characters:",
lowercase)
Q11. Input a string and determine whether it is a
palindrome or not, convert the case of characters in a
string.
Solve.
def count_characters(string):
vowels = 0
consonants = 0
uppercase = 0
lowercase = 0

for char in string:


if char.isalpha():
if char.lower() in 'aeiou':
vowels += 1
else:
consonants += 1
if char.isupper():
uppercase += 1
elif char.islower():
lowercase += 1

return vowels, consonants, uppercase, lowercase


def main():
string = input("Enter a string: ")
vowels, consonants, uppercase, lowercase =
count_characters(string)

print("Number of vowels:", vowels)


print("Number of consonants:", consonants)
print("Number of uppercase characters:",
uppercase)
print("Number of lowercase characters:",
lowercase)
Q12.Find the largest/smallest number in a list/tuple
Solve.
def find_largest_smallest(numbers):
if not numbers:
return None, None

largest = numbers[0]
smallest = numbers[0]
for num in numbers:
if num > largest:
largest = num
elif num < smallest:
smallest = num

return largest, smallest

def main():
numbers = input("Enter a list of numbers
separated by spaces: ").split()
numbers = [float(num) for num in numbers]
largest, smallest = find_largest_smallest(numbers)

if largest is not None and smallest is not None:


print("Largest number:", largest)
print("Smallest number:", smallest)
else:
print("List/tuple is empty.")

if __name__ == "__main__":
main()

Q13.Input a list of numbers and swap elements at


the even location with the elements at the odd
location.
Solve.
def swap_even_odd_elements(numbers):
# Swap elements at even and odd locations
for i in range(0, len(numbers) - 1, 2):
numbers[i], numbers[i + 1] = numbers[i + 1],
numbers[i]

return numbers

def main():
# Input a list of numbers
numbers = input("Enter a list of numbers
separated by spaces: ").split()
numbers = [int(num) for num in numbers] #
Convert input strings to integers

# Ensure the list has even length to avoid


IndexError
if len(numbers) % 2 != 0:
print("Please input a list with an even number
of elements.")
return

# Swap even and odd elements


swapped_numbers =
swap_even_odd_elements(numbers)

print("Original list:", numbers)


print("List after swapping even and odd
elements:", swapped_numbers)
Q14. Input a list/tuple of elements, search for a
given element in the list/tuple
Solve.
def search_element(sequence, element):
if element in sequence:
return True
else:
return False

def main():
sequence_type = input("Enter the type of sequence
(list/tuple): ").lower()

if sequence_type == 'list':
elements = input("Enter a list of elements
separated by spaces: ").split()
sequence = [int(elem) for elem in elements] #
Convert input strings to integers
elif sequence_type == 'tuple':
elements = input("Enter a tuple of elements
separated by spaces: ").split()
sequence = tuple([int(elem) for elem in
elements]) # Convert input strings to integers
else:
print("Invalid sequence type. Please enter 'list'
or 'tuple'.")
return

search_element_value = int(input("Enter the


element to search: "))

if search_element(sequence,
search_element_value):
print("Element", search_element_value, "is
present in the", sequence_type)
else:
print("Element", search_element_value, "is not
present in the", sequence_type)
Q15. Create a dictionary with the roll number, name
and marks of n students in a class and display the
names of students who have marks above 75.
Solve.
def students_above_75_marks(students_dict):
above_75_students = [student['name'] for student
in students_dict.values() if student['marks'] > 75]
return above_75_students

def main():
n = int(input("Enter the number of students: "))
students_dict = {}

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


roll_number = input("Enter roll number of
student {}: ".format(i))
name = input("Enter name of student {}:
".format(i))
marks = float(input("Enter marks of student {}:
".format(i)))
students_dict[roll_number] = {'name': name,
'marks': marks}

above_75_students =
students_above_75_marks(students_dict)

print("Names of students who have marks above


75:")
for name in above_75_students:
print(name)

if __name__ == "__main__":
main()

You might also like