Praticals
Q1. Write a program to enter 2 integers, 2 oating numbers and then perform the arithmetic
operation on them code in python.
int1=int(input("Enter rst integer: "))
int2=int(input("Enter second integer: "))
oat1= oat(input("Enter rst oating-point number: "))
oat2= oat(input("Enter second oating-point number: "))
print("\n--- Arithmetic Operations ---")
print(f"Sum of integers: {int1+int2}")
print(f"Difference of integers: {int1-int2}")
print(f"Product of integers: {int1*int2}")
if int2!=0:
print(f"Division of integers: {int1/int2}")
else:
print("Division of integers: Not possible (division by zero)")
print(f"\nSum of oats: { oat1+ oat2}")
print(f"Difference of oats: { oat1- oat2}")
print(f"Product of oats: { oat1* oat2}")
if oat2!=0:
print(f"Division of oats: { oat1/ oat2}")
else:
print("Division of oats: Not possible (division by zero)”)
Output:
fl
fl
fl
fl
fl
fl
fl
fl
fl
fi
fl
fl
fi
fl
fl
fl
fl
fl
fl
fl
fl
fl
fl
Q2. Write a program to check whether a number is a armstrong number or not.
num = int(input("Enter a number: "))
s=0
n=num
p=len(str(num)) # number of digits
while n>0:
d = n%10
s+=d**p
n//=10
if s==num:
print(num, "is an Armstrong number")
else:
print(num, "is not an Armstrong number”)
Output:
Q3. Write a program to print the sum of all the primes between two ranges.
def is_prime(n):
if n<2:
return False
for i in range(2, int(n**0.5)+1):
if n%i==0:
return False
return True
low=int(input("Enter lower range: "))
high=int(input("Enter upper range: "))
prime_sum=0
for num in range(low, high+1):
if is_prime(num):
prime_sum+=num
print(f"Sum of primes between {low} and {high} is: {prime_sum}”)
Output:
Q4. Write a program to swap two strings.
s1=input("Enter rst string: ")
s2=input("Enter second string: ")
print("\nBefore Swapping:")
print("String 1:", s1)
print("String 2:", s2)
s1,s2=s2,s1
print("\nAfter Swapping:")
print("String 1:",s1)
print("String 2:”,s2)
Output:
fi
Q5. Write a menu driven program to accept two strings from the user and perform the various
function using user de ned functions.
def concatenate(s1,s2):
return s1+s2
def compare(s1,s2):
if s1==s2:
return "Both strings are equal"
elif s1>s2:
return f'"{s1}" is greater than "{s2}" (lexicographically)'
else:
return f'"{s2}" is greater than "{s1}" (lexicographically)'
def reverse(s):
return s[::-1]
def length(s):
return len(s)
def substring(s1,s2):
return "Yes" if s2 in s1 else "No"
s1=input("Enter rst string: ")
s2=input("Enter second string: ")
while True:
print("\n--- String Operations Menu ---")
print("1. Concatenate Strings")
print("2. Compare Strings")
print("3. Reverse Strings")
print("4. Length of Strings")
print("5. Check if Second String is Substring of First")
print("6. Exit")
choice = int(input("Enter your choice: "))
if choice==1:
print("Concatenation:", concatenate(s1, s2))
elif choice==2:
print(compare(s1, s2))
elif choice==3:
print("Reverse of String 1:", reverse(s1))
print("Reverse of String 2:", reverse(s2))
elif choice==4:
print("Length of String 1:", length(s1))
print("Length of String 2:", length(s2))
elif choice==5:
print("Is String 2 a substring of String 1? ->", substring(s1, s2))
elif choice==6:
print("Exiting program. Goodbye!")
break
else:
print("Invalid choice! Please try again.”)
Output:
fi
fi