




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





9.Program to print Fibonacci sequence up to n terms n = int(input("Enter the number of terms: "))
a, b = 0, 1 if n <= 0: print("Please enter a positive integer") elif n == 1: print("Fibonacci sequence up to", n, "term:") print(a) else: print("Fibonacci sequence:") for i in range(n): print(a, end=" ") a, b = b, a + b 10.Write a python Program to check whether a given number is prime or not. num = int(input("Enter a number: ")) if num <= 1: print(num, "is not a prime number") else: for i in range(2, int(num**0.5) + 1): if num % i == 0: print(num, "is not a prime number") break else: print(num, "is a prime number")
11.Program to find the factorial of a number using a user-defined function.
def factorial(n): if n < 0: return "Factorial is not defined for negative numbers" result = 1 for i in range(1, n + 1): result *= i return result
num = int(input("Enter a number: "))
fact = factorial(num) print("Factorial of", num, "is:", fact) 12.Program to find the sum of digits of a given number using a user-defined function.
def sum_of_digits(n): total = 0 while n > 0: digit = n % 10 total += digit n = n // 10 return total
num = int(input("Enter a number: "))
ascii_value = find_ascii(ch) print("The ASCII value of", ch, "is:", ascii_value) 15.Program to check whether a given string is a palindrome or not.
text = input("Enter a string: ")
reversed_text = text[::-1]
if text == reversed_text: print("The string is a palindrome") else: print("The string is not a palindrome") 16.Program to perform the following operations on a list:
lst = list(map(int, input("Enter list elements separated by space: ").split())) print("Original List:", lst)
print("Length of list:", len(lst))
lst.append(10) print("After appending 10:", lst)
lst.insert(1, 5) print("After inserting 5 at index 1:", lst)
if 10 in lst: lst.remove(10) print("After removing 10:", lst)
lst.sort() print("Sorted List:", lst)
lst.reverse() print("Reversed List:", lst) 17.Find the average of elements in the list.
lst = list(map(int, input("Enter list elements separated by space: ").split()))
total = sum(lst) average = total / len(lst) print("List:", lst) print("Average of elements:", average) 18.Write a program to Sort the elements in the list.
lst = list(map(int, input("Enter list elements separated by space: ").split()))
lst.sort()
print("Original tuple:", tup) print("Tuple with even numbers:", even_tup) 21.Write a python program to input an integer โnโ and generate a dictionary containing pairs (i:ii)for i ranging from 1 to โnโ and print the dictionary in a suitable format.*
n = int(input("Enter a number: "))
square_dict = {} for i in range(1, n + 1): square_dict[i] = i * i
print("Generated Dictionary:") print(square_dict) 22.Write a python program to define a class called Rectangle that takes length and breadth as parameters and includes a method to compute its perimeter.
class Rectangle: def init(self, length, breadth): self.length = length self.breadth = breadth
def perimeter(self): return 2 * (self.length + self.breadth)
length = float(input("Enter length: ")) breadth = float(input("Enter breadth: "))
rect = Rectangle(length, breadth)
print("Perimeter of rectangle:", rect.perimeter())