Python Solutions for Programming Questions
1. Print the absolute value of an integer:
num = int(input("Enter a number: "))
print("Absolute value:", abs(num))
2. Check if the input number is odd or even:
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
3. Check if the input number is positive or not:
num = int(input("Enter a number: "))
if num > 0:
print("Positive")
else:
print("Not Positive")
4. Determine the rate of entry-ticket in a trade fair based on age:
age = int(input("Enter age: "))
if age <= 5:
print("Free Entry")
elif 6 <= age <= 12:
print("Child Ticket: $5")
elif 13 <= age <= 59:
print("Adult Ticket: $10")
else:
print("Senior Citizen Ticket: $7")
5. Determine the maximum of two numbers:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Maximum:", max(a, b))
6. Find the smallest of three numbers:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
print("Smallest:", min(a, b, c))
7. Determine whether a year is a leap year or not:
year = int(input("Enter year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap Year")
else:
print("Not a Leap Year")
8. Solve a quadratic equation:
import cmath
a = float(input("Enter coefficient a: "))
b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient c: "))
discriminant = cmath.sqrt(b**2 - 4*a*c)
root1 = (-b + discriminant) / (2 * a)
root2 = (-b - discriminant) / (2 * a)
print("Roots are:", root1, "and", root2)
9. Sum of the first n even numbers:
n = int(input("Enter n: "))
sum_even = sum(range(2, 2*n + 1, 2))
print("Sum of first", n, "even numbers:", sum_even)