practical programs
practical programs
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
# Input values
x = int(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))
# Initialize sum
total = 0
→ 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: "))
🧾 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:
✅ 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
🧾 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
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
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:
# Initialize counters
vowels = 0
consonants = 0
lowercase = 0
uppercase = 0
# 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}")