Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Python

The document provides Python code solutions for 10 programming problems including Armstrong number, Fibonacci series, palindrome checker, inheritance, string conversion, leap year checker, pyramid pattern, matrix multiplication, factorial, and prime numbers. The solutions cover a range of programming concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Python

The document provides Python code solutions for 10 programming problems including Armstrong number, Fibonacci series, palindrome checker, inheritance, string conversion, leap year checker, pyramid pattern, matrix multiplication, factorial, and prime numbers. The solutions cover a range of programming concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Python

Below are Python solutions for the practical practice questions


you've provided:

1. Wap to perform Armstrong number:

def is_armstrong(num):
order = len(str(num))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10
return num == sum

number = int(input("Enter a number: "))


if is_armstrong(number):
print(number, "is an Armstrong number")
else:
print(number, "is not an Armstrong number")

2. Wap to perform Fibonacci series:

def fibonacci(n):
fib_series = [0, 1]
for i in range(2, n):
fib_series.append(fib_series[-1] +
fib_series[-2])
return fib_series[:n]

n = int(input("Enter the number of terms: "))


print("Fibonacci Series:")
print(fibonacci(n))

3. Wap to check for palindrome:

def is_palindrome(s):
return s == s[::-1]

string = input("Enter a string: ")


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

4. Wap to perform hierarchical inheritance:

class Animal:
def __init__(self, name):
self.name = name

def speak(self):
pass

class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!"

dog = Dog("Buddy")
cat = Cat("Whiskers")
print(dog.name, "says", dog.speak())
print(cat.name, "says", cat.speak())

5. Wap to convert any string from lowercase to uppercase:

string = input("Enter a string: ")


uppercase_string = string.upper()
print("Uppercase:", uppercase_string)

6. Wap to check leap year:

def is_leap_year(year):
if (year % 4 == 0 and year % 100 != 0) or (year %
400 == 0):
return True
else:
return False

year = int(input("Enter a year: "))


if is_leap_year(year):
print(year, "is a leap year")
else:
print(year, "is not a leap year")
7. Wap to create a pyramid pattern:

def pyramid_pattern(n):
for i in range(n):
print(" " * (n - i - 1) + "*" * (2 * i + 1))

rows = int(input("Enter the number of rows: "))


pyramid_pattern(rows)

8. Wap to perform matrix multiplication:

def matrix_multiplication(matrix1, matrix2):


result = [[0] * len(matrix2[0]) for _ in
range(len(matrix1))]
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
for k in range(len(matrix2)):
result[i][j] += matrix1[i][k] *
matrix2[k][j]
return result

# Example matrices
matrix1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
matrix2 = [[9, 8, 7], [6, 5, 4], [3, 2, 1]]

result_matrix = matrix_multiplication(matrix1,
matrix2)
for row in result_matrix:
print(row)

9. Wap to perform factorial of a given number:


def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)

number = int(input("Enter a number: "))


print("Factorial of", number, "is",
factorial(number))

10. Wap to perform prime numbers up to n:

def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True

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


print("Prime numbers up to", n, "are:")
for i in range(2, n+1):
if is_prime(i):
print(i, end=" ")

These solutions should cover the practical practice questions you


provided.

You might also like