Sample Programs file.
close() for i in range(1, 11):
print(“File created and message written successfully.”) print(i)
1. Write a program to take two numbers as input and
print their sum. 5. Write a function to calculate the area of a rectangle. 9. Write a program using a while loop to find the sum of
first 10 natural
x = int(input(“Enter first number: “))
def area(length, width): numbers.
y = int(input(“Enter second number: “))
return length * width sum = 0
print(“Sum of”, x, “and”, y, “is”, x + y)
length = int(input(“Enter length: “)) i=1
2. Write a program to check if a number is positive,
negative, or zero. while i <= 10:
width = int(input(“Enter width: “))
num = int(input(“Enter a number: “)) sum += i
print(“Area of the rectangle:”, area(length, width))
if num > 0: i += 1
6. Write a function to check if a number is even or
print(“The number is positive.”) odd. print(“Sum of first 10 natural numbers:”, sum)
elif num == 0: def check_even_odd(num): 10. Write a program to print all even numbers from 1 to
if num % 2 == 0: 20 using a for loop.
print(“The number is zero.”)
return “Even” for i in range(2, 21, 2):
else:
else: print(i)
print(“The number is negative.”)
return “Odd” 11. Write a function to calculate simple interest.
3. Write a program to ask for user age and determine if
they are an adult def simple_interest(principal, time, rate=10):
num = int(input(“Enter a number: “))
or not. return (principal * time * rate) / 100
print(“The number is:”, check_even_odd(num))
age = int(input(“How old are you? “)) p = float(input(“Enter Principal Amount: “))
7. Write a function to return the square and cube of a
if age >= 18: number. t = float(input(“Enter Time in Years: “))
print(“You are an adult!”) def square_and_cube(n): print(“Simple Interest:”, simple_interest(p, t))
else: return n ** 2, n ** 3
print(“You are a teenager or a kid.”) num = int(input(“Enter a number: “))
4. Write a Python program to create a new file and write sq, cb = square_and_cube(num)
a message into it.
print(“Square:”, sq, “Cube:”, cb)
file = open(“[Link]”, “w”)
8. Write a program using a for loop to print numbers
[Link](“Hello, welcome to file handling in Python!”) from 1 to 10.