Lokesh
Lokesh
Lokesh
Solve 1 . #Pattern 1
num_rows = 5
sum = 0
sign = 1
term = sign * (x ** i)
sum += term
return sum
result = series_sum(x, n)
sum = 0
for i in range(1, n + 1) :
term = x ** i / i
sum += term
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 main():
num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))
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
def main():
string = input("Enter a string: ")
vowels, consonants, uppercase, lowercase =
count_characters(string)
largest = numbers[0]
smallest = numbers[0]
for num in numbers:
if num > largest:
largest = num
elif num < smallest:
smallest = num
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 __name__ == "__main__":
main()
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
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
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 = {}
above_75_students =
students_above_75_marks(students_dict)
if __name__ == "__main__":
main()