0% found this document useful (0 votes)
4 views

practical programs

The document contains various Python programs that perform different tasks, such as finding the largest and smallest among three integers, generating patterns, calculating sums of series, and checking properties of numbers (like perfect, Armstrong, and prime). It also includes functions for calculating GCD and LCM, analyzing strings for vowels and consonants, and converting cases. Additionally, it demonstrates how to work with lists and tuples to find maximum and minimum values.

Uploaded by

suganya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

practical programs

The document contains various Python programs that perform different tasks, such as finding the largest and smallest among three integers, generating patterns, calculating sums of series, and checking properties of numbers (like perfect, Armstrong, and prime). It also includes functions for calculating GCD and LCM, analyzing strings for vowels and consonants, and converting cases. Additionally, it demonstrates how to work with lists and tuples to find maximum and minimum values.

Uploaded by

suganya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

# Write a program to find largest among three integers

a=int(input('Enter the first integer:'))


b=int(input('Enter the second integer:'))
c=int(input('Enter the third integer:'))
if a>b and a>c:
print(a, 'is the largest integer')
elif b>a and b>c:
print(b, 'is the largest integer')
else:
print(c, 'is the largest integer')

10. Write a program to find the lowest among the three integers.
# Write a program to find lowest among three integer.
a=int(input('Enter the first integer:'))
b=int(input('Enter the second integer:'))
c=int(input('Enter the third integer:'))
if a<b and a<c:
print(a, 'is the smallest integer')
elif b<a and b<c:
print(b, 'is the smallest integer')
else:
print(c, 'is the smallest integer')
Right angled triangle
rows = 5
for i in range(1, rows + 1):
print("*" * i)

Desired Output:
12345
1234
123
12
rows = 5
for i in range(rows, 0, -1):
for j in range(1, i + 1):
print(j, end=" ")
print()

Desired Output:
A
AB
ABC
ABCD
ABCDE

rows = 5
for i in range(1, rows + 1):
for j in range(i):
print(chr(65 + j), end=" ")
print()

S=1+x+x2+x3+⋯+xn
x = int(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))

total = 0

for i in range(n + 1):


total += x ** i

print("Sum of the series is:", total)

Enter the value of x: 2


Enter the value of n: 3
Sum of the series is: 15
→ Calculation: 1 + 2 + 4 + 8 = 15

# Input values
x = int(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))

# Initialize sum
total = 0

# Loop through terms from 0 to n


for i in range(n + 1):
term = x ** i
# If i is even, add the term; if odd, subtract it
if i % 2 == 0:
total += term
else:
total -= term
# Output the result
print("Sum of the series is:", total)

Enter the value of x: 2


Enter the value of n: 4
Sum of the series is: 7

→ Calculation:
1 - 2 + 4 - 8 + 16 = 11
1 - 2 = -1
-1 + 4 = 3
3 - 8 = -5
-5 + 16 = 11

S=x+2x2−3x3+4x4−5x5+⋯±nxn

This is an alternating series starting from power 1 to power n, with the sign
alternating and each term divided by its power index.

✅ Python Code:
# Input values
x = float(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))

# Initialize the sum


total = 0

# Loop to calculate the sum


for i in range(1, n + 1):
term = (x ** i) / i
if i % 2 == 0:
total += term
else:
total -= term

# Print the result


print("Sum of the series is:", total)

🧾 Example Output:
Enter the value of x: 2
Enter the value of n: 4
Sum of the series is: -0.6666666666666665

Calculation:
S=−2+42−83+164=−2+2−2.666+4=1.333−2.666=−1.333S = -2 + \frac{4}{2} - \
frac{8}{3} + \frac{16}{4} = -2 + 2 - 2.666 + 4 = 1.333 - 2.666 = -1.333S=−2+24
−38+416=−2+2−2.666+4=1.333−2.666=−1.333

You're now asking for a Python program to calculate the sum of this alternating
factorial series:

S=x+x22!−x33!+x44!−⋯±xnn!S = x + \frac{x^2}{2!} - \frac{x^3}{3!} + \


frac{x^4}{4!} - \cdots \pm \frac{x^n}{n!}S=x+2!x2−3!x3+4!x4−⋯±n!xn

✅ Python Code:
import math

# Input values
x = float(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))

# Initialize sum
total = 0

# Loop through 1 to n
for i in range(1, n + 1):
term = (x ** i) / math.factorial(i)
if i % 2 == 0:
total += term # even power → positive
else:
total -= term # odd power → negative

# Print the result


print("Sum of the series is:", total)

🧾 Sample Output:
Enter the value of x: 2
Enter the value of n: 4
Sum of the series is: -0.33333333333333326

Explanation of Terms:

S=−x+x22!−x33!+x44!=−2+42−86+1624=−2+2−1.333+0.666≈−0.667S = -x + \
frac{x^2}{2!} - \frac{x^3}{3!} + \frac{x^4}{4!} = -2 + \frac{4}{2} - \frac{8}
{6} + \frac{16}{24} = -2 + 2 - 1.333 + 0.666 ≈ -0.667S=−x+2!x2−3!x3+4!x4
=−2+24−68+2416=−2+2−1.333+0.666≈−0.667
💡 Tip:

If you want the series to start with +x instead of -x, just adjust the sign logic:
if i % 2 == 0:
total -= term
else:
total += term

# Input from user


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

# Check if the number is a Perfect Number


divisors_sum = sum(i for i in range(1, num) if num % i == 0)
if divisors_sum == num:
print(f"{num} is a Perfect Number")
else:
print(f"{num} is not a Perfect Number")

# Check if the number is an Armstrong Number


num_str = str(num)
num_digits = len(num_str)
sum_of_powers = sum(int(digit) ** num_digits for digit in num_str)
if sum_of_powers == num:
print(f"{num} is an Armstrong Number")
else:
print(f"{num} is not an Armstrong Number")

# Check if the number is a Palindrome


if str(num) == str(num)[::-1]:
print(f"{num} is a Palindrome")
else:
print(f"{num} is not a Palindrome")
# Input from user
num = int(input("Enter a number: "))

# Check if the number is prime or composite


if num < 2:
print(f"{num} is neither prime nor composite.")
else:
# Check if the number is prime
for i in range(2, num):
if num % i == 0:
print(f"{num} is a Composite Number")
break
else:
print(f"{num} is a Prime Number")
Explanation:
Prime Number: A prime number is a number greater than 1 that has no divisors other than 1
and itself.

Composite Number: A composite number is a number greater than 1 that has more than two
divisors (i.e., it's divisible by numbers other than 1 and itself).

Edge Case: The program checks for numbers less than 2, which are neither prime nor
composite (since prime numbers are greater than 1

Here’s a simple Python program to display the terms of a Fibonacci series:

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

a, b = 0, 1

print("Fibonacci Series:")
for a in range(n):
print(a, end=" ")
a, b = b, a + b
Here is a Python program to find the GCD (Greatest Common Divisor) and LCM (Least Common
Multiple) of two integers:

# Function to find GCD


def gcd(a, b):
while b:
a, b = b, a % b
return a

# Function to find LCM


def lcm(a, b):
return abs(a * b) // gcd(a, b)

# Input from user


num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))

# Calculate GCD and LCM


gcd_result = gcd(num1, num2)
lcm_result = lcm(num1, num2)

# Display the results


print(f"The GCD of {num1} and {num2} is: {gcd_result}")
print(f"The LCM of {num1} and {num2} is: {lcm_result}")

ext = input("Enter a string: ")

# Initialize counters
vowels = 0
consonants = 0
lowercase = 0
uppercase = 0

# Define vowels for checking


vowel_set = {'a', 'e', 'i', 'o', 'u'}

# Iterate through each character in the string


for char in text:
if char.isalpha(): # Check if the character is an alphabet
if char.lower() in vowel_set:
vowels += 1
else:
consonants += 1
# Check if the character is lowercase or uppercase
if char.islower():
lowercase += 1
elif char.isupper():
uppercase += 1

# Display the results


print(f"Number of Vowels: {vowels}")
print(f"Number of Consonants: {consonants}")
print(f"Number of Lowercase characters: {lowercase}")
print(f"Number of Uppercase characters: {uppercase}")

text = input("Enter a string: ")


cleaned_text = text.replace(" ", "").lower()
if cleaned_text == cleaned_text[::-1]:
print("The string is a Palindrome.")
else:
print("The string is NOT a Palindrome.")

# Convert case of each character


converted_case = text.swapcase()
print("String after case conversion:", converted_case)

# Ask user how many numbers they want to enter


n = int(input("How many numbers do you want to enter? "))

# Initialize an empty list


num_list = []
# Read integers one by one from user
for i in range(n):
num = int(input(f"Enter number {i+1}: "))
num_list.append(num)

# Convert list to tuple


num_tuple = tuple(num_list)

# Finding largest and smallest in list


max_list = max(num_list)
min_list = min(num_list)

# Finding largest and smallest in tuple


max_tuple = max(num_tuple)
min_tuple = min(num_tuple)

# Display results
print("\nFrom List:")
print(f"Largest Number: {max_list}")
print(f"Smallest Number: {min_list}")

print("\nFrom Tuple:")
print(f"Largest Number: {max_tuple}")
print(f"Smallest Number: {min_tuple}")

You might also like